gpt4 book ai didi

java - 如何使用 bouncycaSTLe 从 pkcs7 文件获取证书?

转载 作者:行者123 更新时间:2023-11-29 09:04:02 24 4
gpt4 key购买 nike

大家好!接下来是我的问题:我有包含预打包 CSR 的 .pkcs7 文件,我想从中获取 CSR。我怎样才能使用 bouncycaSTLe 做到这一点?

我尝试使用 PEMReader,但没有用。


private void getCertificatesPKCS7File(String filename){
try {
certificates = new ArrayList();

FileReader fileReader = new FileReader(filename);
PEMReader pemReader = new PEMReader(fileReader);
// Object obj = pemReader.readObject();
// PKCS10CertificationRequest csr = (PKCS10CertificationRequest) obj;
Object obj = pemReader.readPemObject().getContent();
PKCS10CertificationRequest csr = new PKCS10CertificationRequest(obj);

pemReader.close();

} catch (Exception e) {
e.printStackTrace();
}
}

接下来是异常:


Exception in thread "main" java.lang.IllegalArgumentException: unknown object in factory: org.bouncycastle.asn1.ASN1ObjectIdentifier
at org.bouncycastle.asn1.pkcs.CertificationRequestInfo.getInstance(Unknown Source)
at org.bouncycastle.asn1.pkcs.CertificationRequest.(Unknown Source)
at org.bouncycastle.jce.PKCS10CertificationRequest.(Unknown Source)

谢谢大家!

最佳答案

我找到了解决方案。

KeyStore keystore = KeyStore.getInstance("PKCS12", "BC");
keystore.load (new FileInputStream(PATH+"//test.p12"), "testpassword".toCharArray());
PrivateKey privateKey = (PrivateKey)keystore.getKey("testclientcert", "testpassword".toCharArray());

PEMReader pemReader = new PEMReader(new FileReader(filename));
ContentInfo object = (ContentInfo)pemReader.readObject();

CMSEnvelopedDataParser envDataParser = new CMSEnvelopedDataParser(object.getEncoded());
RecipientInformationStore recipients = envDataParser.getRecipientInfos();
Collection envCollection = recipients.getRecipients();
Iterator it = envCollection.iterator();
RecipientInformation recipient = (RecipientInformation) it.next();
byte[] result = recipient.getContent(privateKey, "BC");
String base64Encoded = new String(Base64.encode(result));

System.out.println(base64Encoded);

base64Encoded 将与编码后的 csr 匹配。

关于java - 如何使用 bouncycaSTLe 从 pkcs7 文件获取证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15937586/

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