Send encrypted and signed email using C#
I want to send an encrypted and signed mail without using any third-party API.
If I send only the alternate view with the signature, Windows Mail can validate it. If I send only with alternate view with encrypted data, Windows Mail can decipher it. But if I send both, Windows Mail gets 2 attachements. If I sign the encryptedBytes
and add those signed bytes to the alternative view it only validates the signature and the message is empty.
Any idea?
MailMessage message = new MailMessage();
message.From = new MailAddress(lblMail.Text);
message.Subject = txtSubject.Text;
string body = "Content-Type: text/plain\r\nContent-Transfer-Encoding: 7Bit\r\n\r\n" + structForm();
byte[] messageData = Encoding.ASCII.GetBytes(body);
ContentInfo content = new ContentInfo(messageData);
EnvelopedCms envelopedCms = new EnvelopedCms(content);
message.To.Add(new MailAddress(provMail));
CmsRecipient recipient = new CmsRecipient(SubjectIdentifierType.SubjectKeyIdentifier, this.certificate);
envelopedCms.Encrypt(recipient);
byte[] encryptedBytes = envelopedCms.Encode();
SignedCms Cms = new SignedCms(new ContentInfo(encryptedBytes));
CmsSigner Signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, new X509Certificate2(@"c:\serv.pfx","123"));
Cms.ComputeSignature(Signer);
byte[] SignedBytes = Cms.Encode();
MemoryStream encryptedStream 开发者_StackOverflow= new MemoryStream(encryptedBytes);
AlternateView encryptedView = new AlternateView(encryptedStream, "application/pkcs7-mime; smime-type=signed--data;name=smime.p7m");
message.AlternateViews.Add(encryptedView);
MemoryStream signedStream = new MemoryStream(SignedBytes);
AlternateView signedView = new AlternateView(signedStream, "application/pkcs7-mime; smime-type=signed-data;name=sig.p7m");
message.AlternateViews.Add(signedView);
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("emailaddress@xpto.com", "XXXXXX");
SmtpClient client = new SmtpClient("smtp.xpto.com");
client.UseDefaultCredentials = false;
client.Credentials = SMTPUserInfo;
client.Send(message);
Label2.Text = "Assinado e cifrado!";
You should sign first, then encrypt.
While the original CMS and S/MIME specifications allow you to do the operations in either order, later work pointed out that signing a document that you can't read is a really bad idea. The signature should be over the plain-text.
The resulting MIME message should only have a single part, which should be S/MIME enveloped-data. Your message has two parts, and the encrypted part is mis-labeled with a signed-data content-type. Create and sign the SignedCms
object. Encode it, and use the encoded value as the content of an EnvelopedCms
object. Encrypt that, and use its encoded value as the content of your MailMessage
, with a content type of "application/pkcs7-mime; smime-type=enveloped-data".
精彩评论