c#中的电子邮件解密

Email decryption in c#

我正在使用 EWS 访问 Exchange 2013 服务器并从该服务器的收件箱收集电子邮件。我需要能够解析在该邮箱收到的电子邮件,其中包括加密和非加密电子邮件。我有用于解密加密电子邮件的 .pfx 文件,但我不确定加密这些电子邮件的正确方法,并且目前在 google 上没有找到任何好的文章。有人可以帮忙吗?

下面是我正在使用的代码示例(请注意,这是在阅读大量文章之后得出的,因此有些内容可能无法按照我认为的方式协同工作)。

        var exchangeEmailHelper = new ExchangeEmailHelper();
        List<EmailMessage> = exchangeEmailHelper.getEmails();

        foreach (var email in emails)
        {
            string[] retValue = null;

            string[] mimeLines = Encoding.UTF8.GetString(email.MimeContent.Content).Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            Console.WriteLine("mimeLines has been read");
            //find out where the encoded message starts
            int ln;
            for (ln = 0; ln < mimeLines.Length; ln++)
            {
                if (mimeLines[ln] == "MIME-Version: 1.0") break;
            }
            Console.WriteLine($"There are {ln} lines until you get to the mime version.");

            StringBuilder sb = new StringBuilder(email.MimeContent.Content.Length);
            for (int sb1 = ln + 1; sb1 < mimeLines.Length; sb1++)
            {
                sb.Append(mimeLines[sb1]);
            }

            var y = Encoding.ASCII.GetBytes(sb.ToString());


            string test1 = Regex.Replace(email.MimeContent.ToString(), @"\t|\n|\r", "");
            test1 = test1.Substring(test1.IndexOf("Content-Transfer-Encoding: base64") + 33);

            var bytearray = Encoding.ASCII.GetBytes(test1);



            var collection = new X509Certificate2Collection();
            collection.Import(ConfigurationManager.AppSettings["certLocation"], ConfigurationManager.AppSettings["certPassword"], X509KeyStorageFlags.PersistKeySet);

            var certificate = collection[0];
            var privateKey = certificate.PrivateKey as RSACryptoServiceProvider;
            var data = privateKey.Decrypt(bytearray, false);

如果您使用 MimeKit:

,这可能会简单得多
MimeMessage message;

using (var stream = new MemoryStream (email.MimeContent.Content, email.MimeContent.Length))
    message = MimeMessage.Load (stream);

var pkcs7 = message.BodyParts.OfType<ApplicationPkcs7Mime> ().FirstOrDefault ();
if (pkcs7 != null) {
    using (var ctx = new TemporarySecureMimeContext ()) {
        using (var stream = File.OpenRead (ConfigurationManager.AppSettings["certLocation"]))
            ctx.Import (stream, ConfigurationManager.AppSettings["certPassword"]);

        // decrypt the MIME part (result will be another MIME entity)
        var decrypted = pkcs7.Decrypt (ctx);

        // The decrypted MIME entity could be a message/rfc822 part (which
        // contains a message), a multipart (such as multipart/mixed) which
        // contains a list of subparts, each with their own content... or it
        // could be a regular MIME part which just has content. Assuming it
        // is just a regular MIME part:
        if (decrypted is MimePart) {
            var part = (MimePart) decrypted;

            using (var stream = File.Create ("decrypted-content.dat"))
                part.Content.DecodeTo (stream);
        }
    }
}