2017-07-24 105 views
0

我有一個PEM格式的字節數組(表示每個證書的鏈)的列表,我想知道是否有方法將它們轉換爲唯一的PKCS7格式的字符串,在Java中。將PEM轉換爲PKCS7(Java)

預先感謝您。

+0

是的,它可以使用bouncycastle。你有什麼嘗試? – pedrofb

+0

Only this http://www.programcreek.com/java-api-examples/index.php?api=org.bouncycastle.util.io.pem.PemWriter(示例5) – amportugal

+1

可能的重複[如何重新打包證書到使用彈性城堡的pkcs#7證書?](https://stackoverflow.com/questions/29638061/how-do-i-repackage-certificates-into-pkcs-7-certificate-using-bouncy-castle) – pedrofb

回答

2

這是一個使用基於answerX509Certificate[]數組構建PKCS#7文件的示例。它不需要私鑰

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

} 
+0

非常感謝,它的工作 – amportugal