大家好!我的问题如下:我正在尝试将 X509 证书加密为 PKCS7,但我收到错误的结果。
第一次尝试是:(用过bcmail-jdk16:1.46)
Security.addProvider(new BouncyCastleProvider());
keystore = KeyStore.getInstance("PKCS12", "BC");
keystore.load (new FileInputStream(PATH+"//pkcs7-csr-cer//identity.p12"), "testpassword".toCharArray());
PrivateKey privateKey = (PrivateKey)keystore.getKey("testclientcert", "testpassword".toCharArray());
CMSSignedDataGenerator signedDataGen = new CMSSignedDataGenerator();
signedDataGen.addSigner(privateKey, certificate, CMSSignedDataGenerator.ENCRYPTION_RSA, CMSSignedDataGenerator.DIGEST_SHA256);
CMSProcessableFile pkcs7 = new CMSProcessableFile(new File(destinationfile));
CMSSignedData signedData = signedDataGen.generate(pkcs7, true, "BC");
signedData = new CMSSignedData(pkcs7, signedData.getEncoded());
......它不起作用。
第二次尝试是 next(使用 bcmail-jdk16-140):
Security.addProvider(new BouncyCastleProvider());
CMSEnvelopedDataGenerator envDataGen = new CMSEnvelopedDataGenerator();
envDataGen.addKeyTransRecipient(certificate);
CMSProcessable sData = new CMSProcessableByteArray(certificate.getEncoded());
CMSEnvelopedData enveloped = envDataGen.generate(sData, CMSEnvelopedDataGenerator.AES256_CBC, "BC");
return enveloped.getEncoded();
在这两种情况下我都会得到错误的结果。请帮助谁知道正确的方法。谢谢!