【发布时间】:2013-08-08 04:26:23
【问题描述】:
我对签名/证书有点陌生,但是在检查了 Google + SO 之后,我找不到答案。 我有为 PKCS #7 分离签名的文件生成签名的基本代码,到目前为止一切都很好......验证签名的客户端对生成的签名感到满意。 我现在有一个新要求,包括使用 S/MIME 签名时间属性对原始文件进行签名的日期/时间。
到目前为止,我处理它的代码是:
final Attribute signingAttribute = new Attribute(CMSAttributes.signingTime, new DERSet(new DERUTCTime(new Date())));
signedAttributes.add(new Attribute(CMSAttributes.contentType, new DERSet(new ASN1ObjectIdentifier("1.2.840.113549.1.7.1"))));
signedAttributes.add(new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(hash))));
signedAttributes.add(signingAttribute);
final AttributeTable signedAttributesTable = new AttributeTable(signedAttributes);
final DefaultSignedAttributeTableGenerator signedAttributeGenerator = new DefaultSignedAttributeTableGenerator(signedAttributesTable);
// now proceed for the signing process with BouncyCastle
final JcaSimpleSignerInfoGeneratorBuilder builder = new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC").setDirectSignature(true);
builder.setSignedAttributeGenerator(signedAttributeGenerator);
final SignerInfoGenerator signerGenerator = builder.build("SHA1withRSA", key, cert);
final CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
...
那么之后的代码和我以前生成签名的代码是一样的……但是不起作用。
对于 messageDigest 的哈希,我并不真正了解:
signedAttributes.add(new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(hash))));
我得到的哈希生成为:
MessageDigest md = MessageDigest.getInstance("SHA1", "BC");
md.update(fileToSign.getBytes("UTF-8"));
hash = md.digest();
但我绝对不确定这是获取哈希的正确方法吗?以及生成 S/MIME 签名时间属性的整体方式...
欢迎对我遗漏的内容提供任何提示或总体解释。
【问题讨论】:
标签: java certificate bouncycastle signing