本文主要是介绍java读取微信p12证书信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;}
}
这篇关于java读取微信p12证书信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!