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

StackOverflow https://stackoverflow.com/questions/4030585

  •  26-09-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top