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;
	}
}
相关推荐
权泽谦几秒前
新世代的 C++:当 ChatGPT 遇上模板元编程
开发语言·c++·chatgpt
MediaTea3 分钟前
Python 第三方库:Flask(轻量级 Web 框架)
开发语言·前端·后端·python·flask
Kuo-Teng6 分钟前
LeetCode 198: House Robber
java·算法·leetcode·职场和发展·动态规划
2501_941111407 分钟前
C++中的状态模式实战
开发语言·c++·算法
小七mod22 分钟前
【Spring】Spring Boot自动配置的案例
java·spring boot·spring·自动配置·源码·ioc·aop
红石榴花生油30 分钟前
Docker + Nginx 部署 Java 项目(JAR 包 + WAR 包)实战笔记
java·tomcat·maven
带刺的坐椅34 分钟前
Solon AI 开发学习 - 1导引
java·ai·openai·solon·mcp
sg_knight35 分钟前
RabbitMQ 中的预取值(prefetch)详解:如何真正提升消费端性能?
java·spring boot·spring·spring cloud·消息队列·rabbitmq·预取值
Dxxyyyy1 小时前
零基础学JAVA--Day34(Map接口+HashTable+HashMap+TreeSet+TreeMap+开发中如何选择集合实现类?(重要))
java·开发语言
rainFFrain1 小时前
qt显示类控件---QProgressBar
开发语言·qt