java读取微信p12证书信息

java 复制代码
import java.security.*;
import java.security.cert.X509Certificate;
public class TestController {
/**
	 * 获取私钥
	 */
	public static void main(String args[]) throws Exception {
		String mchId = "商户号";
		KeyStore ks = KeyStore.getInstance("PKCS12");
		ks.load(getCertStream(""), mchId.toCharArray());
		String alias = ks.aliases().nextElement();
		X509Certificate certificate = (X509Certificate) ks.getCertificate(alias);
		java.util.Date notBefore = certificate.getNotBefore(); // 证书开始有效日期
		java.util.Date notAfter = certificate.getNotAfter(); // 证书结束有效日期
		String serialNo = certificate.getSerialNumber().toString(16).toUpperCase();
		PrivateKey privateKey = (PrivateKey) ks.getKey(alias, mchId.toCharArray());
		System.out.println("证书开始有效日期:" + notBefore);
		System.out.println("证书结束有效日期:" + notAfter);
		System.out.println("---------serialNo------" + serialNo);
		System.out.println("---------privateKey--------" + privateKey);


	}

	/**
	 * 读取证书
	 */
	public static InputStream getCertStream(String certPath) {
		byte[] certData = null;
		try {
		     //通过路径读取
            //InputStream certStream = new FileInputStream(new File(certPath));
            //放到项目resources目录读取
			InputStream certStream = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream("apiclient_cert.p12");
			certData = IOUtils.toByteArray(certStream);
			certStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		ByteArrayInputStream certBis = new ByteArrayInputStream(certData);
		return certBis;
	}
}
相关推荐
那我掉的头发算什么9 小时前
【SpringBoot】统一功能处理详解
java·spring boot·后端·spring
逍遥德9 小时前
编程技能点小记之if-else条件分支合理用法
java·开发语言·代码规范·代码复审·极限编程·代码覆盖率
瞎某某Blinder9 小时前
DFT学习记录[3]:material project api使用方法 mp_api调取与pymatgen保存
java·笔记·python·学习
低调小一9 小时前
Fresco 图片加载全链路解析:从 SimpleDraweeView 到 Producer 责任链
android·开发语言·fresco
_周游9 小时前
Java8 API文档搜索引擎_7.项目优化之权重合并
java·开发语言·前端·搜索引擎·intellij-idea
专注VB编程开发20年9 小时前
c#.NET异步同小,ASYNC,AWAIT,PushFrame ,DOEVENTS
开发语言·.net
电商API_1800790524710 小时前
淘宝商品详情数据获取全方案分享
开发语言·前端·javascript
IT199510 小时前
Java文档阅读笔记-AI LangChain4j - Agent Multiple Tools Calling Example
java·笔记·文档阅读
rlpp10 小时前
spring.profiles.active和spring.profiles.include的使用及区别说明
java·后端·spring
Pluchon10 小时前
硅基计划4.0 算法 简单实现B树
java·数据结构·b树·算法·链表