Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用bouncycastle从pkcs7文件获取证书?_Java_Bouncycastle_Pkcs#7_Csr - Fatal编程技术网

Java 如何使用bouncycastle从pkcs7文件获取证书?

Java 如何使用bouncycastle从pkcs7文件获取证书?,java,bouncycastle,pkcs#7,csr,Java,Bouncycastle,Pkcs#7,Csr,大家好!我的问题是:我有一个.pkcs7文件,它包含预打包的CSR,我想从中获取CSR。我如何使用bouncycastle进行此操作 我试着用PEMReader,但没用 下一个例外是: 线程“main”java.lang.IllegalArgumentException中的异常:工厂中的未知对象:org.bouncycastle.asn1.ASN1ObjectIdentifier 位于org.bouncycastle.asn1.pkcs.CertificationRequestInfo.ge

大家好!我的问题是:我有一个.pkcs7文件,它包含预打包的CSR,我想从中获取CSR。我如何使用bouncycastle进行此操作

我试着用PEMReader,但没用

下一个例外是:


线程“main”java.lang.IllegalArgumentException中的异常:工厂中的未知对象:org.bouncycastle.asn1.ASN1ObjectIdentifier
位于org.bouncycastle.asn1.pkcs.CertificationRequestInfo.getInstance(未知源)
在org.bouncycastle.asn1.pkcs.CertificationRequest上(未知来源)
请访问org.bouncycastle.jce.PKCS10CertificationRequest。(未知来源)

谢谢大家

我找到了解决办法

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将与encoded csr匹配。

您得到的错误是什么?实际上似乎是PKCS12的解决方案?

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);