gpt4 book ai didi

java - 安全异常 : BC provider not installed

转载 作者:搜寻专家 更新时间:2023-10-31 19:39:22 26 4
gpt4 key购买 nike

使用BouncyCaSTLe编写代码生成数字证书。

这是导致问题的代码的重要部分。

public X509Certificate generateCertWithKeypair(KeyPair caPair)
throws InvalidKeyException, SecurityException, SignatureException {
X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();
v3CertGen
.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
v3CertGen
.setIssuerDN(new X509Principal("CN=cn, O=o, L=L, ST=il, C= c"));
v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60
* 60 * 24));
v3CertGen.setNotAfter(new Date(System.currentTimeMillis()
+ (1000L * 60 * 60 * 24 * 365 * 10)));
v3CertGen
.setSubjectDN(new X509Principal("CN=cn, O=o, L=L, ST=il, C= c"));
v3CertGen.setPublicKey(caPair.getPublic());
v3CertGen.setSignatureAlgorithm("SHA1WithRSAEncryption");
X509Certificate generateX509Certificate = v3CertGen
.generateX509Certificate(caPair.getPrivate());//**here**
return generateX509Certificate;
}

面临异常

Exception in "main" java.lang.SecurityException: BC provider not installed!
at X509V3CertificateGenerator.generateX509Certificate(Unknown Source)
at chapter4.Dupe.generateCertWithKeypair(Dupe.java:74)
at chapter4.Dupe.main(Dupe.java:32)

在搜索中我found最新的 jar 解决了这个问题,但没有运气。

我错过了什么吗?

See Full Code Here.

最佳答案

您应该在 JRE 中“注册”BC。您可以通过两种方式完成此操作:将 bcprov.jar 放入 $JRE/lib/ext 文件夹并添加到 $JRE/lib/security/java.security

security.provider.N=org.bouncycastle.jce.provider.BouncyCastleProvider

或者把bcprov.jar放在classpath下,java.security不修改,在某处添加代码

static { Security.addProvider(new BouncyCastleProvider());  }

http://www.bouncycastle.org/wiki/display/JA1/Provider+Installation

关于java - 安全异常 : BC provider not installed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19318582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com