前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java pfx_如何在Java读取PFX格式证书「建议收藏」

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

作者头像
全栈程序员站长
发布2022-06-28 16:37:56
1.2K0
发布2022-06-28 16:37:56
举报
文章被收录于专栏:全栈程序员必看

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

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.cn/132761.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年6月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档