Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instantiate javax.security.X509Certficate object from a p12 certificate (contains certificate + private key)

X509Certificate is only instantiatable using the contents of a certificate (.cer file). How to instantiate this object using a .p12 file that contains both the certificate and the private key?

like image 549
Hugh Darling Avatar asked Oct 27 '10 06:10

Hugh Darling


1 Answers

Here is what you need:

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);
like image 185
WellieeGee Avatar answered Oct 22 '22 21:10

WellieeGee