Java解析X509证书代码

通过PKCS12格式的证书库文件获取证书对象

InputStream inStream = new FileInputStream("c:/certificate.p12");

KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(inStream, "password".toCharArray());  

String alias = ks.aliases().nextElement();
certificate = (X509Certificate) ks.getCertificate(alias);
System.out.println(certificate .getNotAfter());

通过pem文件获取证书对象

CertificateFactory fact = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) fact.generateCertificate(new FileInputStream ("conf/server.crt"));

获取证书的公钥

PublicKey pk = certificate.getPublicKey();

转载于:https://my.oschina.net/yidao620c/blog/3095161

你可能感兴趣的:(Java解析X509证书代码)