5

I am generating CSR using BouncyCastle APIs with bcprov-jdk15on-147 jars.

CertificationRequestInfo certInfo = new CertificationRequestInfo(subject, subKeyInfo, new DERSet(attribute));
org.bouncycastle.operator.ContentSigner sigGen = null;
sigGen = new JcaContentSignerBuilder("SHA1withRSA").setProvider("BC").build(privateKey);
org.bouncycastle.asn1.pkcs.CertificationRequest ctest = new org.bouncycastle.asn1.pkcs.CertificationRequest(certInfo,sigAlgName,new DERBitString(sigGen.getSignature()));

I am passing this obejct to my calling function:

GenerateCSR gcsr = GenerateCSR.getInstance();
System.out.println("Public Key:\n"+gcsr.getPublicKey().toString());

System.out.println("Private Key:\n"+gcsr.getPrivateKey().toString());
org.bouncycastle.asn1.pkcs.CertificationRequest  csr = gcsr.getCSR("IMO");
System.out.println("CSR Request Generated!!");
FileWriter fcsr = new FileWriter("C:\\test.txt");
PEMWriter w1 = new PEMWriter(fcsr);
w1.writeObject(csr);

But i am getting the below exception:

Exception in thread "main" org.bouncycastle.util.io.pem.PemGenerationException: unknown object passed - can't encode.
    at org.bouncycastle.openssl.MiscPEMGenerator.createPemObject(Unknown Source)"
4

2 回答 2

5

更好的解决方案是使用 PemObject。

    String type = "CERTIFICATE REQUEST";
    byte[] encoding = pkcs10.getEncoded();

    PemObject pemObject = new PemObject(type, encoding);

    StringWriter str = new StringWriter();
    PEMWriter pemWriter = new PEMWriter(str);
    pemWriter.writeObject(pemObject);
    pemWriter.close();
    str.close();

    System.out.println(str);
于 2012-07-26T17:35:54.270 回答
1

我最终像这样手动创建了 pem 对象..

    String code = "-----BEGIN CERTIFICATE REQUEST-----\n";
    code += new String(Base64.encodeBase64Chunked(request.getEncoded()));
    code += "-----END CERTIFICATE REQUEST-----";
    System.out.println(code);

我猜这是 PemWriter 无法输出 CertificationRequest 对象的错误,因为看起来 BouncyCastle v1.47 仍然充满了其他错误。

于 2012-04-21T02:54:20.413 回答