How to read public key from PFX file in java

不问归期 提交于 2019-12-08 12:45:52

问题


I am able to read private key from PFX file but not public key. I am using following code to read 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 is working properly when i read from .cer file. Please help


回答1:


Use the KeyStore class and treat the file as a PKCS#12 KeyStore. Use KeyStore.getInstance("PKCS12") to get an instance of a PKCS12 keystore.

The Javadocs for KeyStore contain sample code.



来源:https://stackoverflow.com/questions/4508779/how-to-read-public-key-from-pfx-file-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!