如何从p12证书(包含证书+私钥)实例化javax.security.X509Certficate对象

时间:2010-10-27 06:39:12

标签: x509certificate pkcs#12

X509Certificate只能使用证书(.cer文件)的内容进行实例化。如何使用包含证书和私钥的.p12文件来实例化此对象?

1 个答案:

答案 0 :(得分:8)

以下是您的需求:

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);