gpt4 book ai didi

java - Bouncy CaSTLe - 如何使用 Bouncy CaSTLe 实现 SignedAndEnveloped 数据

转载 作者:行者123 更新时间:2023-12-05 07:32:08 28 4
gpt4 key购买 nike

我想创建一个使用 Bouncy CaSTLe(版本 1.59)实现的 signedAndEnvelopedData (PKCS #7) 数据。

在 Bouncy CaSTLe 中,接口(interface) CMSObjectIdentifiers 包含类型 signedAndEnvelopedData

但是多次尝试都无法正确创建。能否请您提出一些建议,以下是我实现的核心

  1. 首先签署数据
CMSTypedData msg = (CMSTypedData) new CMSProcessableByteArray(
new ASN1ObjectIdentifier(CMSObjectIdentifiers.data.getId()),
srcMsg.getBytes(charSet));

Store certs = new JcaCertStore(certList);

CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
ContentSigner signer = new JcaContentSignerBuilder(
signatureSchema).setProvider("BC").build(privateKey);

gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(
new JcaDigestCalculatorProviderBuilder().setProvider("BC")
.build()).build(signer, cerx509));

gen.addCertificates(certs);

CMSSignedData sigData = gen.generate(msg, true);
sigData = new CMSSignedData(msg,sigData.getEncoded())

return sigData.getEncoded()

这里我将输入数据设置为 CMSTypeData 作为 CMSObjectIdentifiers.data.getId()

CMSTypedData msg = (CMSTypedData) new CMSProcessableByteArray(
new ASN1ObjectIdentifier(CMSObjectIdentifiers.data.getId()),
srcMsg.getBytes(charSet));
  1. 签名数据的输出将用作包络的输入
CMSTypedData msg = new CMSProcessableByteArray(new ASN1ObjectIdentifier(CMSObjectIdentifiers.signedAndEnvelopedData.getId()),srcMsg.getBytes(charSet));

CMSEnvelopedDataGenerator edGen = new CMSEnvelopedDataGenerator();

JcaAlgorithmParametersConverter paramsConverter = new JcaAlgorithmParametersConverter();

edGen.addRecipientInfoGenerator(
new JceKeyTransRecipientInfoGenerator(cert,paramsConverter.getAlgorithmIdentifier(PKCSObjectIdentifiers.id_RSAES_OAEP,OAEPParameterSpec.DEFAULT))
.setProvider(new BouncyCastleProvider()));
OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES256_CBC)
.setProvider(new BouncyCastleProvider())
.build()
CMSEnvelopedData ed = edGen.generate(msg,encryptor)

encryptedContent = ed.getEncoded()
String result = new String(Base64.encode(ed.getEncoded()));

return result;

这里我将输入数据设置为 CMSTypedData 作为 CMSObjectIdentifiers.signedAndEnvelopedData.getId()

CMSTypedData msg = new CMSProcessableByteArray(new ASN1ObjectIdentifier(CMSObjectIdentifiers.signedAndEnvelopedData.getId()),srcMsg.getBytes(charSet));

问题:

  1. Bouncy CaSTLe (1.59) 是否支持 PKCS#7 SignedAndEnevloped
  2. 如果第一个问题是"is",我创建 SignedAndEnevloped 数据的步骤是否正确?
  3. 如果第一个问题是否定的,有什么方法可以实现吗?

最佳答案

我刚刚写了一个关于使用 Bouncy Caslte Provider 在 XMLSignatrure(SignedAndEnevloped) 中执行 RSA 的演示,请参阅这篇文章,https://honwhy.wang/2018/09/07/use-bc-provider-xmlsignature/

演示代码,

1, https://github.com/Honwhy/xml-sec/blob/master/src/main/java/com/honey/xmlsec/BcSignatureAlgorithm.java#L37

2, https://github.com/Honwhy/xml-sec/blob/master/src/main/java/com/honey/xmlsec/MyUtil.java#L107

也许您需要调整一些线路以满足您的要求。

关于java - Bouncy CaSTLe - 如何使用 Bouncy CaSTLe 实现 SignedAndEnveloped 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51474645/

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