久久久久无码精品,四川省少妇一级毛片,老老熟妇xxxxhd,人妻无码少妇一区二区

如何在Java處理PFX格式證書(shū)

時(shí)間:2020-10-04 14:15:37 SUN認(rèn)證 我要投稿

如何在Java處理PFX格式證書(shū)

  公鑰加密技術(shù)12號(hào)標(biāo)準(zhǔn)(Public Key Cryptography Standards #12,PKCS#12)為存儲(chǔ)和傳輸用戶(hù)或服務(wù)器私鑰、公鑰和證書(shū)指定了一個(gè)可移植的格式。它是一種二進(jìn)制格式,這些文件也稱(chēng)為PFX文件。

  開(kāi)發(fā)人員通常需要將PFX文件轉(zhuǎn)換為某些不同的格式,如PEM或JKS,以便可以為使用SSL通信的獨(dú)立Java客戶(hù)端或WebLogic Server使用

  在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是用于存放證書(shū),它是2進(jìn)制形式存放的,不含私鑰。

  .pem跟crt/cer的區(qū)別是它以Ascii來(lái)表示。

  pfx/p12用于存放個(gè)人證書(shū)/私鑰,他通常包含保護(hù)密碼,2進(jìn)制方式

  p10是證書(shū)請(qǐng)求

  p7r是CA對(duì)證書(shū)請(qǐng)求的.回復(fù),只用于導(dǎo)入

  p7b以樹(shù)狀展示證書(shū)鏈(certificate chain),同時(shí)也支持單個(gè)證書(shū),不含私鑰。

  其中,我介紹如何從p12/pfx文件中提取密鑰對(duì)及其長(zhǎng)度:

  1,首先,讀取pfx/p12文件(需要提供保護(hù)密碼)

  2,通過(guò)別名(Alias,注意,所有證書(shū)中的信息項(xiàng)都是通過(guò)Alias來(lái)提取的)提取你想要分析的證書(shū)鏈

  3,再將其轉(zhuǎn)換為一個(gè)以X509證書(shū)結(jié)構(gòu)體

  4,提取里面的項(xiàng),如果那你的證書(shū)項(xiàng)放在第一位(單一證書(shū)),直接讀取 x509Certs[0](見(jiàn)下面的代碼)這個(gè)X509Certificate對(duì)象

  5,X509Certificate對(duì)象有很多方法,tain198127網(wǎng)友希望讀取RSA密鑰(公私鑰)及其長(zhǎng)度(見(jiàn)http://www.matrix.org.cn/thread.shtml?topicId=43786&forumId=55&#reply),那真是太Easy了,

  X509Certificate keyPairCert = x509Certs[0];

  int iKeySize = X509CertUtil.getCertificateKeyLength(keyPairCert);

  System.out.println("證書(shū)密鑰算法="+keyPairCert.getPublicKey().getAlgorithm());

  System.out.println("證書(shū)密鑰長(zhǎng)度="+iKeySize);

  提取了他所需要的信息。

  package org.dev2dev.client.keypair;

  import java.io.File;

  import java.io.FileInputStream;

  import java.io.FileNotFoundException;

  import java.io.IOException;

  import java.security.KeyStore;

  import java.security.KeyStoreException;

  import java.security.NoSuchAlgorithmException;

  import java.security.NoSuchProviderException;

  import java.security.Security;

  import java.security.cert.Certificate;

  import java.security.cert.CertificateException;

  import java.security.cert.X509Certificate;

  import org.dev2dev.security.keytool.X509CertUtil;

  public class LoadKeyFromPKCS12 {

  public static void main(String[] args) {

  try {

  // Open an input stream on the keystore file

  String pfxFileName = " c:\\david.turing.pfx " ;

  String pfxPassword = " 123456 " ;

  File fPkcs12 = null ;

  if (pfxFileName != null ) {

  // Open the file

  fPkcs12 = new File(pfxFileName);

  }

  FileInputStream fis = new FileInputStream(fPkcs12);

  // Create a keystore object

  KeyStore keyStore = null ;

  try

  {

  // Need BC provider for PKCS #12, BKS and UBER

  if (Security.getProvider( " BC " ) == null )

  {

  throw new Exception( " 不能Load入BouncyCastle! " );

  }

  keyStore = KeyStore.getInstance( " PKCS12 " , " BC " );

  }

  catch (KeyStoreException ex)

  {

  throw new Exception( " 不能正確解釋pfx文件! " );

  }

  catch (NoSuchProviderException ex)

  {

  throw new Exception( " Security Provider配置有誤! " );

  }

  try

  {

  // Load the file into the keystore

  keyStore.load(fis, pfxPassword.toCharArray());

  }

  catch (CertificateException ex)

  {

  throw new Exception( " 證書(shū)格式問(wèn)題! " );

  }

  catch (NoSuchAlgorithmException ex)

  {

  throw new Exception( " 算法不支持! " );

  }

  catch (FileNotFoundException ex)

  {

  throw new Exception( " pfx文件沒(méi)找到 " );

  }

  catch (IOException ex)

  {

  throw new Exception( " 讀取pfx有誤! " );

  }

  // 獲取我的證書(shū)鏈的中keyEntry的別名

  Certificate[] certs = keyStore.getCertificateChain( " david.turing " );

  X509Certificate[] x509Certs = X509CertUtil.convertCertificates(certs);

  if (x509Certs == null )

  {

  return ;

  }

  x509Certs = X509CertUtil.orderX509CertChain(x509Certs);

  X509Certificate keyPairCert = x509Certs[ 0 ];

  int iKeySize = X509CertUtil.getCertificateKeyLength(keyPairCert);

  System.out.println( " 證書(shū)密鑰算法= " + keyPairCert.getPublicKey().getAlgorithm());

  System.out.println( " 證書(shū)密鑰長(zhǎng)度= " + iKeySize);

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

【如何在Java處理PFX格式證書(shū)】相關(guān)文章:

Java中日期的處理方法10-05

Java里處理文件的技巧05-08

CAD如何在布局中處理圖形07-25

用Java如何處理XML數(shù)據(jù)05-15

如何在java中解壓zip和rar文件12-05

如何在PHP中處理Protocol Buffers數(shù)據(jù)11-10

關(guān)于Java編程的異常處理特殊情況11-06

sun認(rèn)證java關(guān)于字符串處理技巧11-08

java證書(shū)的加密與解密代碼05-24