如何在Java處理PFX格式證書
公鑰加密技術(shù)12號(hào)標(biāo)準(zhǔn)(Public Key Cryptography Standards #12,PKCS#12)為存儲(chǔ)和傳輸用戶或服務(wù)器私鑰、公鑰和證書指定了一個(gè)可移植的格式。它是一種二進(jìn)制格式,這些文件也稱為PFX文件。
開發(fā)人員通常需要將PFX文件轉(zhuǎn)換為某些不同的格式,如PEM或JKS,以便可以為使用SSL通信的獨(dú)立Java客戶端或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是用于存放證書,它是2進(jìn)制形式存放的,不含私鑰。
.pem跟crt/cer的區(qū)別是它以Ascii來表示。
pfx/p12用于存放個(gè)人證書/私鑰,他通常包含保護(hù)密碼,2進(jìn)制方式
p10是證書請(qǐng)求
p7r是CA對(duì)證書請(qǐng)求的.回復(fù),只用于導(dǎo)入
p7b以樹狀展示證書鏈(certificate chain),同時(shí)也支持單個(gè)證書,不含私鑰。
其中,我介紹如何從p12/pfx文件中提取密鑰對(duì)及其長(zhǎng)度:
1,首先,讀取pfx/p12文件(需要提供保護(hù)密碼)
2,通過別名(Alias,注意,所有證書中的信息項(xiàng)都是通過Alias來提取的)提取你想要分析的證書鏈
3,再將其轉(zhuǎn)換為一個(gè)以X509證書結(jié)構(gòu)體
4,提取里面的項(xiàng),如果那你的證書項(xiàng)放在第一位(單一證書),直接讀取 x509Certs[0](見下面的代碼)這個(gè)X509Certificate對(duì)象
5,X509Certificate對(duì)象有很多方法,tain198127網(wǎng)友希望讀取RSA密鑰(公私鑰)及其長(zhǎng)度(見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("證書密鑰算法="+keyPairCert.getPublicKey().getAlgorithm());
System.out.println("證書密鑰長(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( " 證書格式問題! " );
}
catch (NoSuchAlgorithmException ex)
{
throw new Exception( " 算法不支持! " );
}
catch (FileNotFoundException ex)
{
throw new Exception( " pfx文件沒找到 " );
}
catch (IOException ex)
{
throw new Exception( " 讀取pfx有誤! " );
}
// 獲取我的證書鏈的中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( " 證書密鑰算法= " + keyPairCert.getPublicKey().getAlgorithm());
System.out.println( " 證書密鑰長(zhǎng)度= " + iKeySize);
} catch (Exception e) {
e.printStackTrace();
}
}
}
【如何在Java處理PFX格式證書】相關(guān)文章:
Java中日期的處理方法10-05
Java里處理文件的技巧05-08
CAD如何在布局中處理圖形07-25
如何在java中解壓zip和rar文件12-05
如何在PHP中處理Protocol Buffers數(shù)據(jù)11-10
sun認(rèn)證java關(guān)于字符串處理技巧11-08
java證書的加密與解密代碼05-24