How to read public key from PFX file in java

I can read the private key from the PFX file, but not the public key. I am using the following code to read the public key.

InputStream inStream = new FileInputStream(certFile); CertificateFactory cf = CertificateFactory.getInstance("X.509"); BufferedInputStream bis = new BufferedInputStream(inStream); // if (bis.available() > 0) { java.security.cert.Certificate cert = cf.generateCertificate(bis); System.out.println("This part is not getting printed in case of PFX file"); // } puk = (PublicKey) cert.getPublicKey(); 

This code works correctly when I read the .cer file. Please, help

0
source share
1 answer

Use the KeyStore class and process the file as KeyStore PKCS # 12. Use KeyStore.getInstance("PKCS12") to get an instance of the PKCS12 key store.

Javadocs for KeyStore contains sample code.

+3
source

Source: https://habr.com/ru/post/1347858/


All Articles