gpt4 book ai didi

java - 从 Bouncy CaSTLe 中的 ASN.1 编码检索 CMSSignedData

转载 作者:太空宇宙 更新时间:2023-11-04 11:23:01 25 4
gpt4 key购买 nike

在下面的代码中,我使用 Bouncy CaSTLe 签署了一条消息:

import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;
import org.bouncycastle.cms.CMSTypedData;
import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
import org.bouncycastle.util.encoders.Base64;

import java.io.FileInputStream;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.PKCS8EncodedKeySpec;

public class Sign {

public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());

String certPath = "certPath";
FileInputStream inPublic = new FileInputStream(certPath);
CertificateFactory factory = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) factory.generateCertificate(inPublic);


String keyPrivatePath = "keyPath";
Path path = Paths.get(keyPrivatePath);
Files.readAllBytes(Paths.get(keyPrivatePath));
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Files.readAllBytes(Paths.get(keyPrivatePath)));
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privateKey = kf.generatePrivate(spec);

CMSProcessableByteArray msg = new CMSProcessableByteArray("My message".getBytes());
CMSSignedDataGenerator sGen = new CMSSignedDataGenerator();

ContentSigner sha1Signer = new JcaContentSignerBuilder("SHA1withRSA").setProvider("BC").build(privateKey);
sGen.addSignerInfoGenerator(
new JcaSignerInfoGeneratorBuilder(
new JcaDigestCalculatorProviderBuilder().setProvider("BC").build()
).build(sha1Signer, cert)
);

CMSSignedData sd = sGen.generate(msg);

CMSTypedData cmsBytes = new CMSProcessableByteArray(sd.getEncoded());
// How to reconstruct a CMSSignedData from cmsBytes again?
byte[] bytes = (byte[]) cmsBytes.getContent();
CMSSignedData retrieved = new CMSSignedData(bytes);
System.out.println(retrieved.getSignedContent()); // Doesn't work, is null
}
}

我的问题是如何仅使用该对象的 ASN.1 编码的字节数组来检索原始 CMSSignedData (想要读取原始消息并验证它)。

我问这个问题的原因是我想解密某个加密和签名的消息。我能够解密此消息,但它会生成 ASN.1 编码的字节数组(它确实对应于我的原始消息),但我无法进一步处理此解密的消息。

最佳答案

您可以使用类org.bouncycaSTLe.asn1.cms.ContentInfoorg.bouncycaSTLe.asn1.ASN1Sequence:

CMSTypedData cmsBytes = new CMSProcessableByteArray(sd.getEncoded());
byte[] bytes = (byte[]) cmsBytes.getContent();

// reconstruct CMSSignedData from the byte array
ContentInfo ci = ContentInfo.getInstance(ASN1Sequence.fromByteArray(bytes));
CMSSignedData sig = new CMSSignedData(ci);

另请注意,您必须创建一个 CMSSignedData,并将内容封装在签名中,因此您必须更改此设置:

CMSSignedData sd = sGen.generate(msg);

对此:

CMSSignedData sd = sGen.generate(msg, true);

关于java - 从 Bouncy CaSTLe 中的 ASN.1 编码检索 CMSSignedData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44704289/

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