我使用了以下源代码并成功生成了PKCS7编码的PEM,但我需要生成基于RFC7468 https://tools.ietf.org/html/rfc7468#page-11编码的CMS。基于RFC,建议使用CMS编码的PEM。如果您知道答案,请提前帮助,谢谢。
ContentSigner sha256Signer = new JcaContentSignerBuilder("SHA256WITHECDSA").setProvider(new org.spongycastle.jce.provider.BouncyCastleProvider()).build(key);
CMSTypedData msg = new CMSProcessableByteArray(data.getBytes(charset));
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(
new JcaDigestCalculatorProviderBuilder().build()).build(
sha256Signer, new X509CertificateHolder(x509Cert)));
gen.addCertificates(certStore);
CMSSignedData signedData = gen.generate(msg, true);
// Generate PKCS7 PEM
ContentInfo ci = signedData.toASN1Structure();
StringWriter signedCertificatePEMDataStringWriter = new StringWriter();
JcaPEMWriter pemWrt = new JcaPEMWriter(signedCertificatePEMDataStringWriter);
pemWrt.writeObject(ci);
pemWrt.flush();
pemWrt.close();