java pfx_如何在Java读取PFX格式证书「建议收藏」

java pfx_如何在Java读取PFX格式证书「建议收藏」X509CertificatekeyPairCert=x509Certs[0];intiKeySize=X509CertUtil.getCertificateKeyLength(keyPairCert);System.out.println(“证书密钥算法=”+keyPairCert.getPublicKey().getAlgorithm());System.out.println(“…

大家好,又见面了,我是你们的朋友全栈君。

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();

}

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/132761.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号