2010-08-18 81 views
5

我正在嘗試將簽名時間屬性添加到使用SignedCMS簽名的文件中。爲PKCS7簽名CMS添加簽名時間?

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; 
} 

會發生錯誤的編碼:

CryptographicException: The object identifier is poorly formatted. 

如何正確添加簽約時間任何想法?我想我可能必須將簽名時間轉換爲ASN.1編碼對象,並將其添加到cryptoAtty的值。如何將日期/時間轉換爲ASN.1編碼對象?

回答

10

alt text

那麼這很容易。

cmsSigner.SignedAttributes.Add(new Pkcs9SigningTime());