2017-02-21 81 views
0

我正在嘗試使用EWS閱讀數字簽名電子郵件的內容。不幸的是,當我使用的方法與EnvelopeCMS我得到一個異常:EWS - 數字簽名電子郵件(smime.p7m)

System.Security.Cryptography.CryptographicException:ASN1 - 壞標籤 價值滿足。

在System.Security.Cryptography.Pkcs.EnvelopedCms.OpenToDecode(字節[] encodedMessage)
在System.Security.Cryptography.Pkcs.EnvelopedCms.Decode(字節[] encodedMessage)在myExchange.Email.DecryptToFile(

Byte [] data)

(encodedMessage是smime.p7m電子郵件的附件)。

編輯:這是一個關鍵的代碼片段:

foreach (Attachment attachment in emailMessage.Attachments) 
{ 
    if (attachment is FileAttachment) 
    { 
     FileAttachment fileAttachment = attachment as FileAttachment; 

     if (fileAttachment.Name == "smime.p7m")       
     { 
       byte[] content = fileAttachment.Content; 

       MemoryStream stream = new MemoryStream(); 
       fileAttachment.Load(stream); 
       StreamReader stReader = new StreamReader(stream); 
       stream.Seek(0, SeekOrigin.Begin); 
       content = stream.GetBuffer(); 

       var encrypted = new System.Security.Cryptography.Pkcs.EnvelopedCms(); 
       encrypted.Decode(content); // <==== Here occurs exception 

       encrypted.Decrypt(); 
       byte[] unencryptedButRawMimeEntity = encrypted.ContentInfo.Content; 
     } 
    } 
} 

更多的電子郵件 - EWS輸出控制檯說,它有一個附件「mutipart /簽訂了」內涵式

<m:ResponseCode>NoError</m:ResponseCode> 
      <m:Attachments> 
       <t:FileAttachment> 
       <t:AttachmentId Id="AAMkADNi(... CUT ...)T5PWd/bDM=" /> 
       <t:Name>smime.p7m</t:Name> 
       <t:ContentType>multipart/signed</t:ContentType> 
+0

歡迎堆棧溢出!如果您發佈代碼,則可能會獲得更多更好的幫助。發佈代碼時,確保它是[最小,完整且可驗證的示例](http://stackoverflow.com/help/mcve)。 –

+0

試試只是fileAttachment.Load();然後fileAttachment.Content將成爲附件。然後嘗試解密的東西......也許顯示簽名的含義。你現在正在使用附件,而不是電子郵件不確定是否簽名和/或意味着什麼。 – Seabizkit

回答

0

沒有測試過,所以請讓我知道我會想象這樣的事情...

foreach (Attachment attachment in emailMessage.Attachments) 
{ 
    FileAttachment fileAttachment = attachment as FileAttachment 
    if (attachment != null) 
    {   
     fileAttachment.Load(); 
     if (fileAttachment.Name == "smime.p7m")       
     { 
       byte[] content = fileAttachment.Content; 

       var encrypted = new EnvelopedCms(); 
       encrypted.Decode(content); 
       encrypted.Decrypt(); 
       byte[] unencryptedButRawMimeEntity = encrypted.ContentInfo.Content; 
     } 
    } 
} 
+0

這簡化了代碼,但結果相同 - 在encrypted.Decode()操作上發生異常「ASN1 - 壞標記值已滿足」。 我工作的附件,因爲我的目標是從這樣的電子郵件獲取/檢查附件。它在Outlook中被標記爲「已簽名」,並且在其中有一個PDF附件。對於其他電子郵件代碼工作正常,但簽名的看起來像他們有一個「smime.p7m」附件裏面。 –

+0

更多關於電子郵件:EWS控制檯輸出說,它有一個FileAttachment Content-Type =「multipart/signed」 –

+0

我不知道,但繼這個http://stackoverflow.com/questions/21629206/ews-retrieving - 從簽名的電子郵件,這肯定你已經看過......是他建議你需要解碼包含附件而不是附件本身的原始郵件。 Sozs發現很難遵循也許它對你更有意義。 – Seabizkit