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

前端之家收集整理的这篇文章主要介绍了c# – 为PKCS7签名CMS添加签名时间?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将签名时间属性添加到我使用SignedCMS签名的文件中.
  1. private byte[] signFile(byte[] fileContent,X509Certificate2 verificationCert)
  2. {
  3. ContentInfo contentInfo = new ContentInfo(fileContent);
  4.  
  5. SignedCms signedCMS = new SignedCms(contentInfo);
  6.  
  7. CmsSigner cmsSigner = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber,verificationCert);
  8.  
  9. Oid signedDate = new Oid("1.2.840.113549.1.9.5"); //oid for PKCS #9 signing time
  10.  
  11. signedDate.Value = DateTime.Now.ToString();
  12.  
  13. CryptographicAttributeObject cryptoAtty = new CryptographicAttributeObject(signedDate);
  14.  
  15. cmsSigner.SignedAttributes.Add(cryptoAtty);
  16.  
  17. signedCMS.ComputeSignature(cmsSigner,false);
  18.  
  19. byte[] encoded = signedCMS.Encode();
  20.  
  21. return encoded;
  22. }

在Encode上抛出错误

  1. CryptographicException: The object identifier is poorly formatted.

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

解决方法

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

猜你在找的C#相关文章