大家好,又见面了,我是你们的朋友全栈君。
X509Certificate keyPairCert = x509Certs[0];
int iKeySize = X509CertUtil.getCertificateKeyLength(keyPairCert);
System.out.println(“证书密钥算法=”+keyPairCert.getPublicKey().getAlgorithm());
System.out.println(“证书密钥长度=”+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( ” 证书密钥长度= ” + iKeySize);
} catch (Exception e) {
e.printStackTrace();
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/132761.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...