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;
	}
}
相关推荐
清心歌7 分钟前
Java SE线程的创建
java
高兴达32 分钟前
Spring boot入门工程
java·spring boot·后端
Ronin30533 分钟前
【C++】类型转换
开发语言·c++
萧曵 丶33 分钟前
Spring @TransactionalEventListener
java·数据库·spring·事务·transactional·异步
笑衬人心。34 分钟前
HTTPS详解:原理 + 加解密过程 + 面试问答
java·网络协议·http·面试·https
蓝澈112136 分钟前
弗洛伊德(Floyd)算法-各个顶点之间的最短路径问题
java·数据结构·动态规划
再见晴天*_*44 分钟前
logback 日志不打印
java·服务器·logback
mrbone111 小时前
Git-git worktree的使用
开发语言·c++·git·cmake·worktree·gitab
幽络源小助理1 小时前
SpringBoot基于JavaWeb的城乡居民基本医疗信息管理系统
java·spring boot·学习
欧阳有财1 小时前
[java八股文][Mysql面试篇]日志
java·mysql·面试