加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为PKCS7签名CMS添加签名时间?

发布时间:2020-12-15 08:31:48 所属栏目:百科 来源:网络整理
导读:我正在尝试将签名时间属性添加到我使用SignedCMS签名的文件中. private byte[] signFile(byte[] fileContent,X509Certificate2 verificationCert){ ContentInfo contentInfo = new ContentInfo(fileContent); SignedCms signedCMS = new SignedCms(contentIn
我正在尝试将签名时间属性添加到我使用SignedCMS签名的文件中.
private byte[] signFile(byte[] fileContent,X509Certificate2 verificationCert)
{
   ContentInfo contentInfo = new ContentInfo(fileContent);

   SignedCms signedCMS = new SignedCms(contentInfo);

   CmsSigner cmsSigner = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber,verificationCert);

   Oid signedDate = new Oid("1.2.840.113549.1.9.5"); //oid for PKCS #9 signing time 

   signedDate.Value = DateTime.Now.ToString();

   CryptographicAttributeObject cryptoAtty = new CryptographicAttributeObject(signedDate);

   cmsSigner.SignedAttributes.Add(cryptoAtty);

   signedCMS.ComputeSignature(cmsSigner,false);

   byte[] encoded = signedCMS.Encode();

   return encoded;
}

在Encode上抛出错误:

CryptographicException: The object identifier is poorly formatted.

有关如何正确添加签名时间的任何想法?我想我可能必须将签名时间转换为ASN.1编码对象并将其添加到cryptoAtty的值.如何将日期/时间转换为ASN.1编码对象?

解决方法

那很容易.
cmsSigner.SignedAttributes.Add(new Pkcs9SigningTime());

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读