将PEM转换为PKCS7(Java) [英] Convert PEM to PKCS7 (Java)
本文介绍了将PEM转换为PKCS7(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个PEM格式的字节数组列表(代表链中的每个证书),我想知道是否有一种方法可以用Java将其转换为唯一的PKCS7格式的字符串。
I have a List of Byte arrays (representing each certificate from a chain), in PEM format, and I would like to know if there's a way to convert these to a unique PKCS7 formatted String, in Java.
谢谢。
推荐答案
这是构建PKCS#的示例7文件,使用基于此答案的 X509Certificate []
数组。它不需要私钥
This is an example to build a PKCS#7 file using a X509Certificate[]
array based on this answer. It does not require the private key
//Export a certificate list to PKCS#7
public static byte[] exportCertificatesAsPkcs7(X509Certificate certs[]) throws Exception {
List certList = new ArrayList();
for (X509Certificate certificate: certs){
certList.add(new X509CertificateHolder(certificate.getEncoded()));
}
Store certStore = new JcaCertStore(certList);
CMSProcessableByteArray msg = new CMSProcessableByteArray("Hello World".getBytes());
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
gen.addCertificates(certStore);
CMSSignedData data = gen.generate(msg, "BC");
return data.getEncoded();
}
这篇关于将PEM转换为PKCS7(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文