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;
	}
}
相关推荐
笑衬人心。几秒前
在 Mac 上安装 Java 和 IntelliJ IDEA(完整笔记)
java·macos·intellij-idea
SimonKing7 分钟前
颠覆传统IO:零拷贝技术如何重塑Java高性能编程?
java·后端·程序员
sniper_fandc18 分钟前
SpringBoot系列—MyBatis(xml使用)
java·spring boot·mybatis
胚芽鞘6811 小时前
查询依赖冲突工具maven Helper
java·数据库·maven
Charlie__ZS1 小时前
若依框架去掉Redis
java·redis·mybatis
screenCui1 小时前
macOS运行python程序遇libiomp5.dylib库冲突错误解决方案
开发语言·python·macos
linux kernel1 小时前
第七讲:C++中的string类
开发语言·c++
咖啡啡不加糖1 小时前
RabbitMQ 消息队列:从入门到Spring Boot实战
java·spring boot·rabbitmq
玩代码2 小时前
Java线程池原理概述
java·开发语言·线程池
NE_STOP2 小时前
SpringBoot--如何给项目添加配置属性及读取属性
java