gpt4 book ai didi

java - 使用 Bouncy CaSTLe Java 生成 X509 证书

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:51:31 24 4
gpt4 key购买 nike

我正在寻找在 Java 中使用 BC 生成 X509 证书的示例或教程。

很多示例都拥有/使用已弃用的 API。我查看了 BC,但它没有显示哪个类做了什么或没有适当的文档/示例。

如果您对此有任何想法,请指点我可以使用 BC 生成 X509 证书的教程。 [生成并写入公钥和私钥到文件]

最佳答案

KeyPairGenerator 的创建:

private KeyPairGenerator createKeyPairGenerator(String algorithmIdentifier,
int bitCount) throws NoSuchProviderException,
NoSuchAlgorithmException {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(
algorithmIdentifier, BouncyCastleProvider.PROVIDER_NAME);
kpg.initialize(bitCount);
return kpg;
}

key 对的创建:

private KeyPair createKeyPair(String encryptionType, int byteCount)
throws NoSuchProviderException, NoSuchAlgorithmException
{
KeyPairGenerator keyPairGenerator = createKeyPairGenerator(encryptionType, byteCount);
KeyPair keyPair = keyPairGenerator.genKeyPair();
return keyPair;
}

KeyPair keyPair = createKeyPair("RSA", 4096);

将事物转换为 PEM(可以写入文件):

  private String convertCertificateToPEM(X509Certificate signedCertificate) throws IOException {
StringWriter signedCertificatePEMDataStringWriter = new StringWriter();
JcaPEMWriter pemWriter = new JcaPEMWriter(signedCertificatePEMDataStringWriter);
pemWriter.writeObject(signedCertificate);
pemWriter.close();
return signedCertificatePEMDataStringWriter.toString();
}

创建 X509 证书:

X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(
serverCertificate, new BigInteger("1"),
new Date(System.currentTimeMillis()),
new Date(System.currentTimeMillis() + 30L * 365L * 24L * 60L * 60L * 1000L),
jcaPKCS10CertificationRequest.getSubject(),
jcaPKCS10CertificationRequest.getPublicKey()
/*).addExtension(
new ASN1ObjectIdentifier("2.5.29.35"),
false,
new AuthorityKeyIdentifier(keyPair.getPublic().getEncoded())*/
).addExtension(
new ASN1ObjectIdentifier("2.5.29.19"),
false,
new BasicConstraints(false) // true if it is allowed to sign other certs
).addExtension(
new ASN1ObjectIdentifier("2.5.29.15"),
true,
new X509KeyUsage(
X509KeyUsage.digitalSignature |
X509KeyUsage.nonRepudiation |
X509KeyUsage.keyEncipherment |
X509KeyUsage.dataEncipherment));

签名:

    ContentSigner sigGen = new JcaContentSignerBuilder("SHA256withRSA").build(signingKeyPair.getPrivate());


X509CertificateHolder x509CertificateHolder = certificateBuilder.build(sigGen);
org.spongycastle.asn1.x509.Certificate eeX509CertificateStructure =
x509CertificateHolder.toASN1Structure();
return eeX509CertificateStructure;
}

private X509Certificate readCertificateFromASN1Certificate(
org.spongycastle.asn1.x509.Certificate eeX509CertificateStructure,
CertificateFactory certificateFactory)
throws IOException, CertificateException { //
// Read Certificate
InputStream is1 = new ByteArrayInputStream(eeX509CertificateStructure.getEncoded());
X509Certificate signedCertificate =
(X509Certificate) certificateFactory.generateCertificate(is1);
return signedCertificate;
}

证书工厂:

    certificateFactory = CertificateFactory.getInstance("X.509",
BouncyCastleProvider.PROVIDER_NAME);

关于java - 使用 Bouncy CaSTLe Java 生成 X509 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14930381/

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