using System; using System.Configuration; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Net.Mail; using System.Net.Mime; using System.Security.Cryptography.Pkcs; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SendMessage(); } public void SendMessage() { encrypt(signed(buildMessageContent())); } public void encrypt(string content) { MailMessage message = new System.Net.Mail.MailMessage(); string encryptedContentType = "application/x-pkcs7-mime; smime-type=enveloped-data; name=\"smime.p7m\""; string signatureBoundry2 = "--PTBoundry=3"; StringBuilder fullUnencryptedMessageBuilder = new StringBuilder(); fullUnencryptedMessageBuilder.Append("Content-Type: "); fullUnencryptedMessageBuilder.Append("multipart/signed; "); fullUnencryptedMessageBuilder.Append("boundary=\""); fullUnencryptedMessageBuilder.Append(signatureBoundry2); fullUnencryptedMessageBuilder.Append("\"; protocol=\"application/x-pkcs7-signature\"; micalg=SHA1; "); fullUnencryptedMessageBuilder.Append("\r\n"); fullUnencryptedMessageBuilder.Append("Content-Transfer-Encoding: "); fullUnencryptedMessageBuilder.Append(TransferEncoding.SevenBit); fullUnencryptedMessageBuilder.Append("\r\n\r\n"); fullUnencryptedMessageBuilder.Append(content); string fullUnencryptedMessage = fullUnencryptedMessageBuilder.ToString(); byte[] encryptedBytes = DoEncrypt(fullUnencryptedMessage, GetCert()); MemoryStream stream = new MemoryStream(encryptedBytes); AlternateView view = new AlternateView(stream, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m"); view.TransferEncoding = TransferEncoding.Base64; message.AlternateViews.Add(view); message.To.Add("yourmail@yourdomain.com"); message.From = new MailAddress("someone@yourdomain.com"); message.Subject = "TEST"; SmtpClient smtp = new SmtpClient("Smtp-relay.ip.nianet.dk"); smtp.Send(message); } public byte[] DoEncrypt(string message, X509Certificate2 encryptionCertificates) { byte[] messageBytes = Encoding.ASCII.GetBytes(message); EnvelopedCms envelopedCms = new EnvelopedCms(new ContentInfo(messageBytes)); CmsRecipient recipients = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, encryptionCertificates); envelopedCms.Encrypt(recipients); return envelopedCms.Encode(); } public string signed(string Content) { string signatureBoundry = "--PTBoundry=2"; string signatureBoundry2 = "--PTBoundry=3"; StringBuilder fullUnsignedMessageBuilder = new StringBuilder(); fullUnsignedMessageBuilder.Append("Content-Type: "); fullUnsignedMessageBuilder.Append("multipart/mixed;"); fullUnsignedMessageBuilder.Append(" boundary=\""); fullUnsignedMessageBuilder.Append(signatureBoundry); fullUnsignedMessageBuilder.Append("\"\r\n"); fullUnsignedMessageBuilder.Append("Content-Transfer-Encoding: "); fullUnsignedMessageBuilder.Append("7bit"); fullUnsignedMessageBuilder.Append("\r\n"); fullUnsignedMessageBuilder.Append(Content); string fullUnsignedMessage = fullUnsignedMessageBuilder.ToString(); byte[] signature = GetSignature(fullUnsignedMessage, GetCert(), GetCert()); StringBuilder signedMessageBuilder = new StringBuilder(); signedMessageBuilder.Append("--"); signedMessageBuilder.Append(signatureBoundry2); signedMessageBuilder.Append("\r\n"); signedMessageBuilder.Append(fullUnsignedMessage); signedMessageBuilder.Append("\r\n"); signedMessageBuilder.Append("--"); signedMessageBuilder.Append(signatureBoundry2); signedMessageBuilder.Append("\r\n"); signedMessageBuilder.Append("Content-Type: application/x-pkcs7-signature; name=\"smime.p7s\"\r\n"); signedMessageBuilder.Append("Content-Transfer-Encoding: base64\r\n"); signedMessageBuilder.Append("Content-Disposition: attachment; filename=\"smime.p7s\"\r\n\r\n"); signedMessageBuilder.Append(Convert.ToBase64String(signature)); signedMessageBuilder.Append("\r\n\r\n"); signedMessageBuilder.Append("--"); signedMessageBuilder.Append(signatureBoundry2); signedMessageBuilder.Append("--\r\n"); return signedMessageBuilder.ToString(); } public byte[] GetSignature(string message, X509Certificate2 signingCertificate, X509Certificate2 encryptionCertificate) { byte[] messageBytes = Encoding.ASCII.GetBytes(message); SignedCms signedCms = new SignedCms(new ContentInfo(messageBytes), true); CmsSigner cmsSigner = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, signingCertificate); cmsSigner.IncludeOption = X509IncludeOption.WholeChain; if (encryptionCertificate != null) { cmsSigner.Certificates.Add(encryptionCertificate); } Pkcs9SigningTime signingTime = new Pkcs9SigningTime(); cmsSigner.SignedAttributes.Add(signingTime); signedCms.ComputeSignature(cmsSigner, false); return signedCms.Encode(); } public string buildMessageContent() { string messageBoundry = "--PTBoundry=2"; StringBuilder message = new StringBuilder(); message.Append("\r\n"); message.Append("\r\n"); message.Append("--"); message.Append(messageBoundry + "\r\n"); message.Append("Content-Type: text/html; charset=us-ascii\r\n"); //could use text/html as well here if you want a html message message.Append("Content-Transfer-Encoding: "); message.Append("quoted - printable"); message.Append("\r\n\r\n"); message.Append("TEST AF kryptering");//BODY TEXT GOES HERE message.Append("\r\n"); //ADD file section //could be filename or whatever //foreach (string filename in attachments){ //Read file part implement your own byte[] buff = null; FileStream fs = new FileStream("c:\\snebaer.jpg", FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); long numBytes = new FileInfo("c:\\snebaer.jpg").Length; buff = br.ReadBytes((int)numBytes); byte[] bytes = buff; //Setup filecontent String filecontent = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks); message.Append("--"); message.Append(messageBoundry); message.Append("\r\n"); message.Append("Content-Type: "); message.Append("application/octet-stream;"); message.Append("name=c:\\snebaer.jpg"); message.Append("\r\n"); message.Append("Content-Transfer-Encoding: base64\r\n\r\n"); message.Append(filecontent); message.Append("\r\n\r\n"); //} //END FILSECTION message.Append("--"); message.Append(messageBoundry); message.Append("--\r\n"); return message.ToString(); } public static X509Certificate2 GetCert() { //Sets up a new store to look for at certificat in. X509Store localStore = new X509Store(StoreName.My, StoreLocation.LocalMachine); localStore.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); try { //NOTE FALSE IS ONLY USED FOR TESTS SHOULD BE CHANGED TO true
X509Certificate2Collection matches = localStore.Certificates.Find(X509FindType.FindBySerialNumber, "a0 51 bf 0a bc 4a 11 8b 41 9d 56 47 92 b2 34 6c", false); if (matches.Count > 0) { return matches[0]; } else { return null; } } finally { localStore.Close(); } } }