虫虫技术在线--技术决定出路

当前位置: 首页 > 编程 > Java >

Java处理PFX格式证书

时间:2010-04-05 08:03来源:虫虫技术在线收集整理 作者:虫虫编辑 点击:
在Security编程中,有几种典型的密码交换信息文件格式: DER-encoded certificate: .cer, .crt PEM-encoded message: .pem PKCS#12 Personal Information Exchange: .pfx, .p12 PKCS#10 Certification Request: .p10 PKCS#7 cert request

  在Security编程中,有几种典型的密码交换信息文件格式:
    DER-encoded certificate: .cer, .crt
    PEM-encoded message: .pem
    PKCS#12 Personal Information Exchange: .pfx, .p12
    PKCS#10 Certification Request: .p10
    PKCS#7 cert request response: .p7r
    PKCS#7 binary message: .p7b

    .cer/.crt是用于存放证书,它是2进制形式存放的,不含私钥。
    .pem跟crt/cer的区别是它以Ascii来表示。
    pfx/p12用于存放个人证书/私钥,他通常包含保护密码,2进制方式
    p10是证书请求
    p7r是CA对证书请求的回复,只用于导入
    p7b以树状展示证书链(certificate chain),同时也支持单个证书,不含私钥。

    其中,我介绍如何从p12/pfx文件中提取密钥对及其长度:
    1,首先,读取pfx/p12文件(需要提供保护密码)
    2,通过别名(Alias,注意,所有证书中的信息项都是通过Alias来提取的)提取你想要分析的证书链
    3,再将其转换为一个以X509证书结构体
    4,提取里面的项,如果那你的证书项放在第一位(单一证书),直接读取 x509Certs[0](见下面的代码)这个X509Certificate对象
    5,X509Certificate对象有很多方法,tain198127网友希望读取RSA密钥(公私钥)及其长度(见http://www.matrix.org.cn/thread.shtml?topicId=43786&forumId=55&#reply),那真是太Easy了,

  1. X509Certificate keyPairCert = x509Certs[0];  
  2. int iKeySize = X509CertUtil.getCertificateKeyLength(keyPairCert);  
  3. System.out.println("证书密钥算法="+keyPairCert.getPublicKey().getAlgorithm());  
  4. System.out.println("证书密钥长度="+iKeySize);  

    提取了他所需要的信息。
 

  1. package  org.dev2dev.client.keypair;  
  2.  
  3.  import  java.io.File;  
  4.  import  java.io.FileInputStream;  
  5.  import  java.io.FileNotFoundException;  
  6.  import  java.io.IOException;  
  7.  import  java.security.KeyStore;  
  8.  import  java.security.KeyStoreException;  
  9.  import  java.security.NoSuchAlgorithmException;  
  10.  import  java.security.NoSuchProviderException;  
  11.  import  java.security.Security;  
  12.  import  java.security.cert.Certificate;  
  13.  import  java.security.cert.CertificateException;  
  14.  import  java.security.cert.X509Certificate;  
  15.  import  org.dev2dev.security.keytool.X509CertUtil;  
  16.  
  17.  
  18.  public   class  LoadKeyFromPKCS12   {  
  19.  
  20.      public   static   void  main(String[] args)   {  
  21.          try    {  
  22.              //  Open an input stream on the keystore file  
  23.              String pfxFileName = " c:\\david.turing.pfx " ;  
  24.             String pfxPassword = " 123456 " ;  
  25.  
  26.             File fPkcs12  =   null ;  
  27.              if  (pfxFileName  !=   null )   {  
  28.                  //  Open the file  
  29.                  fPkcs12  =   new  File(pfxFileName);  
  30.             }  
  31.  
  32.  
  33.             FileInputStream fis  =   new  FileInputStream(fPkcs12);  
  34.  
  35.              //  Create a keystore object  
  36.              KeyStore keyStore  =   null ;  
  37.              try 
  38.                {  
  39.                      //  Need BC provider for PKCS #12, BKS and UBER  
  40.                       if  (Security.getProvider( " BC " )  ==   null )  
  41.                       {  
  42.                          throw   new  Exception( " 不能Load入BouncyCastle! " );  
  43.                     }  
  44.  
  45.                     keyStore  =  KeyStore.getInstance( " PKCS12 " ,  " BC " );  
  46.             }  
  47.              catch  (KeyStoreException ex)  
  48.               {  
  49.                   throw   new  Exception( " 不能正确解释pfx文件! " );  
  50.             }  
  51.              catch  (NoSuchProviderException ex)  
  52.               {  
  53.                  throw   new  Exception( " Security Provider配置有误! " );  
  54.             }  
  55.  
  56.              try 
  57.                {  
  58.                  //  Load the file into the keystore  
  59.                  keyStore.load(fis, pfxPassword.toCharArray());  
  60.             }  
  61.              catch  (CertificateException ex)  
  62.               {  
  63.                  throw   new  Exception( " 证书格式问题! " );  
  64.             }  
  65.              catch  (NoSuchAlgorithmException ex)  
  66.               {  
  67.                  throw   new  Exception( " 算法不支持! " );  
  68.                 }  
  69.              catch  (FileNotFoundException ex)  
  70.               {  
  71.                  throw   new  Exception( " pfx文件没找到 " );  
  72.             }  
  73.              catch  (IOException ex)  
  74.               {  
  75.                  throw   new  Exception( " 读取pfx有误! " );  
  76.             }  
  77.  
  78.              // 获取我的证书链的中keyEntry的别名  
  79.              Certificate[] certs  =  keyStore.getCertificateChain( " david.turing " );  
  80.             X509Certificate[] x509Certs  =  X509CertUtil.convertCertificates(certs);  
  81.  
  82.              if  (x509Certs  ==   null )  
  83.               {  
  84.                  return ;  
  85.             }  
  86.  
  87.             x509Certs  =  X509CertUtil.orderX509CertChain(x509Certs);  
  88.  
  89.             X509Certificate keyPairCert  =  x509Certs[ 0 ];  
  90.  
  91.              int  iKeySize  =  X509CertUtil.getCertificateKeyLength(keyPairCert);  
  92.             System.out.println( " 证书密钥算法= " + keyPairCert.getPublicKey().getAlgorithm());  
  93.             System.out.println( " 证书密钥长度= " + iKeySize);  
  94.  
  95.         }   catch  (Exception e)   {  
  96.             e.printStackTrace();  
  97.         }  
  98.     }  
  99.  
  100. }  

 

(责任编辑:admin)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
推荐内容