大家好!我的问题是下一个:我有.pkcs7文件,其中包含预先打包的CSR,我想从中获取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)
谢谢所有人!
答案 0 :(得分:0)
我找到了解决方案。
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匹配。