EWS - 数字签名电子邮件(smime.p7m)

问题描述:

我正在尝试使用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

没有测试过,所以请让我知道我会想象这样的事情...

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