【问题标题】:Send encrypted and signed email using a public and private key for submit data in FMCSA使用公钥和私钥发送加密和签名的电子邮件,以便在 FMCSA 中提交数据
【发布时间】:2019-02-28 21:16:44
【问题描述】:

我正在使用 Mime 工具包发送邮件和加密。代码是,

   public void SendMail(string filePath)
   {

         var message = new MimeMessage();

            message.From.Add(new MailboxAddress("Test", "xxx@gmail.com"));
            message.To.Add(new MailboxAddress("Test Mail", "xx@xx.xx"));
            message.To.Add(new MailboxAddress("Test", "xxx@xx.com"));
            message.Subject = "TEST: ";

           var subject  = "TEST:";
            var body = new TextPart("plain")
            {
                Text = "Sample comments"
            };

            var attachment = new MimeKit.MimePart("multipart/related", "txt")
            {
                Content = new MimeContent(File.OpenRead(filePath), ContentEncoding.Default),
                ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
                ContentTransferEncoding = ContentEncoding.Base64,
                FileName = Path.GetFileName(filePath)
            };

            var multipart = new Multipart("mixed");
            multipart.Add(body);
            multipart.Add(attachment);
            message.Body = multipart;

                var certificateFile = @"E:\RESDE_RSA.pfx";
                var certificate = new X509Certificate2(certificateFile,"", X509KeyStorageFlags.Exportable);


                var recipientCollection = new CmsRecipientCollection();
                var bountyRecipientCertificate = DotNetUtilities.FromX509Certificate(certificate);

                var recipient = new CmsRecipient(bountyRecipientCertificate);
                recipient.EncryptionAlgorithms = new EncryptionAlgorithm[] { EncryptionAlgorithm.Aes256 };
                recipientCollection.Add(recipient);              

            using (var client = new MailKit.Net.Smtp.SmtpClient())
            {
                client.Connect("smtp.gmail.com", 25, false);

                client.Authenticate("xx@gmail.com", "password");

                client.Send(message);
                client.Disconnect(true);
            }
}

邮件发送成功,但邮件的加密和签名不正确。我没有得到任何关于如何解密和签署消息的解决方案。 我正在使用 MVC 应用程序。

【问题讨论】:

    标签: c# encryption mimekit


    【解决方案1】:

    首先,这是不正确的:

    var attachment = new MimeKit.MimePart("multipart/related", "txt")
    

    这将导致:

    Content-Type: multipart/related/txt
    

    那是完全错误的 mime 类型。

    不确定您要做什么,但绝对不应该是 multipart 任何东西。

    现在是关于为什么您的邮件没有被加密的主要问题。

    简单:您正在执行获取 CMS 收件人列表的前几个步骤(看起来不错),但您没有对他们做任何事情。

    var certificateFile = @"E:\RESDE_RSA.pfx";
    var certificate = new X509Certificate2(certificateFile,"", X509KeyStorageFlags.Exportable);
    
    var recipientCollection = new CmsRecipientCollection();
    var bountyRecipientCertificate = DotNetUtilities.FromX509Certificate(certificate);
    
    var recipient = new CmsRecipient(bountyRecipientCertificate);
    recipient.EncryptionAlgorithms = new EncryptionAlgorithm[] { EncryptionAlgorithm.Aes256 };
    recipientCollection.Add(recipient);
    
    // now you need to actually encrypt
    using (var ctx = new TemporarySecureMimeContext ()) {
        var encrypted = ApplicationPkcs7Mime.Encrypt (ctx, recipientCollection, multipart);
        message.Body = encrypted;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2017-01-04
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 2022-08-06
      相关资源
      最近更新 更多