使用BouncyCastle API生成CSR

Fox*_*Fox 15 java security bouncycastle certificate

我是Java的安全方面的新手,偶然发现了这个名为bouncycastle的库.但他们提供的示例和互联网上的示例要求使用 -

     return new PKCS10CertificationRequest("SHA256withRSA", new X500Principal(
    "CN=Requested Test Certificate"), pair.getPublic(), null, pair.getPrivate()
Run Code Online (Sandbox Code Playgroud)

但是当我使用PKCS10CertificationRequest时,看起来它已被弃用.所以我开始研究另一种使用CertificationRequest类的方法.但我真的很困惑,构造函数不采用相同的参数,而是需要CertificationRequestInfo类,我不知道如何填写.

    CertificationRequest request = new CertificationRequest(...);
Run Code Online (Sandbox Code Playgroud)

如果有人可以帮我弄清楚如何制作CSR以便我可以将其发送到服务器以获得签名,那将是非常棒的.

谢谢,

Jcs*_*Jcs 25

使用最新版本的BouncyCastle,建议使用org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder该类创建CSR .

您可以使用此代码snipppet:

KeyPair pair = generateKeyPair();
PKCS10CertificationRequestBuilder p10Builder = new JcaPKCS10CertificationRequestBuilder(
    new X500Principal("CN=Requested Test Certificate"), pair.getPublic());
JcaContentSignerBuilder csBuilder = new JcaContentSignerBuilder("SHA256withRSA");
ContentSigner signer = csBuilder.build(pair.getPrivate());
PKCS10CertificationRequest csr = p10Builder.build(signer);
Run Code Online (Sandbox Code Playgroud)

  • 对于新代码,我建议使用JcaPEMWriter而不是PEMWriter.相同的界面,但现在不推荐使用PEMWriter. (3认同)
  • 我知道了..在PEMWriter类的帮助下完成了..谢谢您的帮助。 (2认同)
  • @VikramSinghShekhawat 您可以在 X500Principal 中添加这些信息。例如 new X500Principal("CN=请求的测试证书,O=Test Inc,C=US") (2认同)