C#中的电子邮件解密

时间:2018-12-27 15:44:11

标签: c# encryption cryptography exchangewebservices smime

我正在使用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);

1 个答案:

答案 0 :(得分:1)

如果您使用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);
        }
    }
}