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;
	}
}
相关推荐
再写一行代码就下班3 分钟前
linux sh脚本在windows修改导致无法使用解决方式
java·linux·centos
ShyanZh38 分钟前
【Python3基础】13-Python 常用设计模式
开发语言·python·设计模式
m0_3807438744 分钟前
Qt侧边栏布局的实现示例
开发语言·c++
m0_587383001 小时前
24小时自助健身房软硬件解决方案实战指南:从架构设计到部署实施
java·spring·小程序·架构·需求分析
zh_xuan1 小时前
c++ 23 std::out_ptr用法
开发语言·c++23·out_ptr
陆卿之1 小时前
Java对接DeepSeek
java·开发语言·人工智能
庄园特聘拆椅狂魔1 小时前
Java 后端转全栈的第一课:从前端项目搭建到技术选
java·开发语言·前端
烽火聊员1 小时前
Android屏幕卡顿traces.txt拉取查看
android·java·android studio
lzhdim2 小时前
C#不为人知的10个魔法特性:资深开发者也会震惊的底层奥秘
java·前端·javascript·算法·c#
weixin_307779132 小时前
C++代码实现MATLAB中的pade函数功能
开发语言·c++·算法·matlab