2022如何在Java处理PFX格式证书如何在Java处理PFX格式证书公钥加密技术12号标准(PublicKeyCryptographyStandards#12,PKCS#12)为存储和传输用户或服务器私钥、公钥和证书指定了一个可移植的格式。它是一种二进制格式,这些文件也称为PFX文件。开发人员通常须要将PFX文件转换为某些不同的格式,如PEM或JKS,以便可以为运用SSL通信的独立Java客户端或WebLogicServer运用在Security编程中,有几种典型的密码交换信息文件格式:第1页共15页DER-encodedcertificate:.cer,.crtPEM-encodedmessage:.pemPKCS#12PersonalInformationExchange:.pfx,.p12PKCS#10CertificationRequest:.p10PKCS#7certrequestresponse:.p7rPKCS#7binarymessage:.p7b.cer/.crt是用于存放证书,它是2进制形式存放的,不含私钥。.pem跟crt/cer的区分是它以Ascii来表示。pfx/p12用于存放个人证书/私钥,他通常包含爱护密码,2进制第2页共15页方式p10是证书恳求p7r是CA对证书恳求的.回复,只用于导入p7b以树状展示证书链(certificatechain),同时也支持单个证书,不含私钥。其中,我介绍如何从p12/pfx文件中提取密钥对及其长度:1,首先,读取pfx/p12文件(须要供应爱护密码)2,通过别名(Alias,留意,全部证书中的信息项都是通过Alias来提取的)提取你想要分析的证书链第3页共15页3,再将其转换为一个以X509证书结构体4,提取里面的项,假如那你的证书项放在第一位(单一证书),干脆读取x509Certs[0](见下面的代码)这个X509Certificate对象5,X509Certificate对象有许多方法,tain198127网友希望读取RSA密钥(公私钥)及其长度(见http://www.matrix.org.cn/thread.shtml?topicId=43786&forumId=55reply),那真是太Easy了,X509CertificatekeyPairCert=x509Certs[0];intiKeySize=第4页共15页X509CertUtil.getCertificateKeyLength(keyPairCert);System.out.println("证书密钥算法="+keyPairCert.getPublicKey().getAlgorithm());System.out.println("证书密钥长度="+iKeySize);提取了他所须要的信息。packageorg.dev2dev.client.keypair;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;第5页共15页importjava.io.IOException;importjava.security.KeyStore;importjava.security.KeyStoreException;importjava.security.NoSuchAlgorithmException;importjava.security.NoSuchProviderException;importjava.security.Security;importjava.security.cert.Certificate;importjava.security.cert.CertificateException;importjava.security.cert.X509Certificate;第6页共15页importorg.dev2dev.security.keytool.X509CertUtil;publicclassLoadKeyFromPKCS12{publicstaticvoidmain(String[]args){try{//OpenaninputstreamonthekeystorefileStringpfxFileName="c:\\david.turing.pfx";StringpfxPassword="123456";FilefPkcs12=null;第7页共15页if(pfxFileName!=null){//OpenthefilefPkcs12=newFile(pfxFileName);}FileInputStreamfis=newFileInputStream(fPkcs12);//CreateakeystoreobjectKeyStorekeyStore=null;try{第8页共15页//NeedBCproviderforPKCS#12,BKSandUBERif(Security.getProvider("BC")==null){thrownewException("不能Load入BouncyCastle!");}keyStore=KeyStore.getInstance("PKCS12","BC");}第9页共15页catch(KeyStoreExceptionex){thrownewException("不能正确说明pfx文件!");}catch(NoSuchProviderExceptionex){thrownewException("SecurityProvider配置有误!");第10页共15页}try{//LoadthefileintothekeystorekeyStore.load(fis,pfxPassword.toCharArray());}catch(CertificateExceptionex){thrownewException("证书格式问题!");第11页共15页}catch(NoSuchAlgorithmExceptionex){thrownewException("算法不支持!");}catch(FileNotFoundExceptionex){thrownewException("pfx文件没找到");}第12页共15页catch(IOExceptionex){thrownewException("读取pfx有误!");}//获得我的证书链的中keyEntry的别名Certificate[]certs=keyStore.getCertificateChain("david.turing");X509Certificate[]x509Certs=X509CertUtil.convertCertificates(certs);第13页共15页if(x509Certs==null){return;}x509Certs=X509CertUtil.orderX509CertChain(x509Certs);X509CertificatekeyPairCert=x509Certs[0];intiKeySize=X509CertUtil.getCertificateKeyLength(keyPairCert);System.out.println("证书密钥算法="+第14页共15页keyPairCert.getPublicKey().getAlgorithm());System.out.println("证书密钥长度="+iKeySize);}catch(Exceptione){e.printStackTrace();}}}本文来源:网络收集与整理,如有侵权,请联系作者删除,谢谢!第15页共15页