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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 避免在移动端页面中使用100vh

    避免在移动端页面中使用100vh100vh带来的问题在CSS中,视口单位(Viewportunits)听起来不错。如果要设置一个元素的样式使它占据整个屏幕的高度,那么你可以设置height:100vh,这样你就拥有一个完美的全屏元素,该元素会随着视口的变化而调整大小!可惜的是,事实并非如此。100vh在移动浏览器中以一种微妙但基本的方式被破坏,使其几乎无用。最好避免使用100vh,而应该通过javascript设置高度的方…

    2022年5月1日
    43
  • idea打包详解_vue打包后图片不显示

    idea打包详解_vue打包后图片不显示1.点击File->ProjectStructure2.选择打包类型3.指定jar包运行的mainclass,并指定生META-INF文件的位置(一般放在resource目录下)4.配置依赖包的存放目录:鼠标右击<outputroot>创建libs文件夹,并将依赖包拖入libs文件夹(注:如果更改了依赖包的位置,classpath中的内容必须做出对应的修改)5.检查各项配置无误选择ok:框选位置依次表示为jar包名;jar输出位置;指定的编译文件,ma

    2022年10月3日
    1
  • 1nv04dp是什么芯片_电平转换芯片总容易弄混淆了

    1nv04dp是什么芯片_电平转换芯片总容易弄混淆了可实现3.3V到5V或者5V到3.3V的电平转换共分两组8位  1B1到1B8(5V)对应1A1到1A8(3.3V),可通过1DIR控制电平转换方向2B1到2B8(5V)对应2A1到2A8(3.3V),可通过1DIR控制电平转换方向

    2022年8月10日
    3
  • exit与return的区别

    exit与return的区别

    2021年11月3日
    52
  • java资源网站总汇[通俗易懂]

    java资源网站总汇[通俗易懂]java资源网: http://www.javaresource.org/ 

    2022年7月8日
    33
  • Lnmp修改php.ini配置

    Lnmp修改php.ini配置

    2021年10月9日
    34

发表回复

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

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