从加密/签名邮件下载附件

时间:2018-03-26 14:07:44

标签: c# exchangewebservices smime mimekit

我正在尝试处理已签名和加密的电子邮件。更准确地说,我想下载附加的Excel文件。

据我所知,我拥有一个pfx文件(带密码),这是一个包含要解密的私钥的存档,以及一个用于验证邮件发件人的证书(公钥)。

修改

jstedfast的帮助下,我现在提出一个有用的最小例子。

/* certificates */
X509Certificate2 certificate = new X509Certificate2(
    privateKeyFullFileName,
    privateKeyPassword,
    X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Add(certificate);

/* smtp handling */
FindItemsResults<Item> inboxEMails = exchangeService.FindItems(WellKnownFolderName.Inbox, new ItemView(int.MaxValue));
EmailMessage eMail = // find appropriate mail
EmailMessage tmp = EmailMessage.Bind(ExchangeService, eMail.Id, new PropertySet(BasePropertySet.FirstClassProperties));
foreach (Attachment attachment in tmp.Attachments) {
  FileAttachment fa = attachment as FileAttachment;
  fa.Load();
  /* decrypting */
  using (MemoryStream rawStream = new MemoryStream(fa.Content)) {
    EnvelopedCms envelopedCms = new EnvelopedCms();
    envelopedCms.Decode(rawStream.ToArray());
    envelopedCms.Decrypt(collection); // <- decrypting with .pfx file
    using (MemoryStream decryptedStream = new MemoryStream(envelopedCms.ContentInfo.Content)) {
      MimeKit.MimeEntity entity = MimeKit.MimeEntity.Load(decryptedStream);
      if (entity is MimeKit.Cryptography.ApplicationPkcs7Mime) {
        MimeKit.Cryptography.ApplicationPkcs7Mime p7m = entity as MimeKit.Cryptography.ApplicationPkcs7Mime;
        using (MimeKit.Cryptography.TemporarySecureMimeContext ctx = new MimeKit.Cryptography.TemporarySecureMimeContext()) {
          Org.BouncyCastle.X509.X509Certificate cert = Org.BouncyCastle.Security.DotNetUtilities.FromX509Certificate(certificate);
            ctx.Import(cert);
            if (p7m.SecureMimeType == MimeKit.Cryptography.SecureMimeType.SignedData) {
              MimeKit.MimeEntity extracted;
              MimeKit.Cryptography.DigitalSignatureCollection signatures = p7m.Verify(ctx, out extracted);
              MimeKit.MimePart part = extracted as MimeKit.MimePart;
              if (part != null) {
                // TODO
              }
              MimeKit.Multipart multipart = extracted as MimeKit.Multipart;
              if (multipart != null) {
                for (int i = 0; i < multipart.Count; ++i) {
                MimeKit.MimePart mimePart = multipart.ElementAt(i) as MimeKit.MimePart;
                if (mimePart != null && mimePart.IsAttachment) {
                  using (FileStream outStream = File.Create(/* path + */ mimePart.FileName)) {
                    try {
                      mimePart.Content.DecodeTo(outStream);
                    } catch { }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

@app.route('/test', methods=['POST'], content_types=['application/json'], cors=cors_config) def post_item(data): data = app.current_request.json_body return data 开始并假设它是decryptedStream MIME部分,您开始正确,但您要做的不是调用application/pkcs7-mime; smime-type=signed-data而是调用{{1}而是:

Import()