使用JAVA使用BouncyCastle签署CAdES

根据我发现的几个post,现在可以使用BouncyCastle执行CAdES,但几乎没有关于该主题的任何文档。

对于初学者,我想在具有基于文件的证书的文件上执行CAdES-BES而没有任何可选的签名属性。


为了回应皮屑:

我有一些可能有用的东西,你有你的SignerInformation,你需要扩展它,首先你需要从时间戳创建一个属性,我假设你已经有一个TimeStampResponse作为tspResp

TimeStampToken token = tsresp.getTimeStampToken(); Attribute timeStamp = new Attribute(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken, new DERSet(ASN1Object.fromByteArray(token.getEncoded()))); 

然后你需要扩展你的SignerInformation

 AttributeTable unsigned = signerInformation.getUnsignedAttributes(); Hashtable unsignedAttrHash = null; if (unsigned == null) { unsignedAttrHash = new Hashtable(); } else { unsignedAttrHash = signerInformation.getUnsignedAttributes().toHashtable(); } unsignedAttrHash.put(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken, signatureTimeStamp); SignerInformation newsi = SignerInformation.replaceUnsignedAttributes(si, new AttributeTable( unsignedAttrHash)); 

我认为就是这样。

以下是我获得signin-certificate属性的方法


 Attribute signingCertificateAttribute; MessageDigest dig = MessageDigest.getInstance(DigestAlgorithm().getName(), new BouncyCastleProvider()); byte[] certHash = dig.digest(SigningCertificate().getEncoded()); if (DigestAlgorithm() == DigestAlgorithm.SHA1) { SigningCertificate sc = new SigningCertificate(new ESSCertID(certHash)); signingCertificateAttribute = new Attribute(PKCSObjectIdentifiers.id_aa_signingCertificate, new DERSet(sc)); } else { ESSCertIDv2 essCert = new ESSCertIDv2(new AlgorithmIdentifier(DigestAlgorithm().getOid()), certHash); SigningCertificateV2 scv2 = new SigningCertificateV2(new ESSCertIDv2[] { essCert }); signingCertificateAttribute = new Attribute(PKCSObjectIdentifiers.id_aa_signingCertificateV2, new DERSet(scv2)); } 

希望能帮助到你

CAdES是CMS(又名PKCS7)的扩展,可以与BouncyCastle一起使用。 RFC5126包含CAdES签名所需的所有内容,同样,我建议在ASN.1上查找信息,因为大多数部分都是以该格式描述的。

我目前正在寻找您正在寻找的相同答案,并发现David Hook的“ Beginning Cryptography with Java ”一书提供了您可能需要的大量详细信息。

有用的代码可以在“ https://joinup.ec.europa.eu/ ”上找到

看看CAdESProfileBES.java 。

有人在原始SD – 数字签名服务的Fork上放了相同的代码。