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

时间:2017-02-21 20:30:39

标签: email exchangewebservices decoding smime

我正在尝试使用EWS阅读经过数字签名的电子邮件内容。不幸的是,当我使用EnvelopeCMS的方法时,我得到一个例外:

  

System.Security.Cryptography.CryptographicException:ASN1 - 错误标记   价值得到满足。

     System.Security.Cryptography.Pkcs.EnvelopedCms.OpenToDecode(Byte []中的

  encodedMessage)
  在myExchange.Email.DecryptToFile(Byte [] data)中的System.Security.Cryptography.Pkcs.EnvelopedCms.Decode(Byte [] encodedMessage)中

(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 / signed”内容类型的附件

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

1 个答案:

答案 0 :(得分: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;
         }
    }
}