2

I have been tasked with encrypting a string using S/Mime encryption. Eons ago, the firm I work for bought a component for this (from IPWorks) but we have had untold bundles of grief getting their component to play nicely on our servers. Not a functionality issue, more licensing.

So in short, I must do it myself. I have trawled the MSDN and forums and put together the following code. Unfortunately the output it creates is not what I expect. Lots of Korean and special characters that I would not expect.

public string EncryptString(string toEncrypt, string key)
{
// Convert the body to bytes
byte[] bodyBytes = Encoding.ASCII.GetBytes(toEncrypt);

// Encrypt the body
var envelopedCms = new EnvelopedCms(new ContentInfo(bodyBytes));

var certificate = new X509Certificate2(Encoding.ASCII.GetBytes(key));

var recipient = new CmsRecipient(certificate);
envelopedCms.Encrypt(recipient);
byte[] encryptedBytes = envelopedCms.Encode();
var msg = new MailMessage();
var ms = new MemoryStream(encryptedBytes);
var av = new AlternateView(ms, "application/pkcs7-mime; smime-type=enveloped-data;name=smime.p7m; content-transfer-encoding=Base64; content-disposition=attachment; fileName=smime.p7m;");
msg.AlternateViews.Add(av);

return new StreamReader(msg.AlternateViews[0].ContentStream).ReadToEnd();
}

Can anyone see an obvious blunder here?

I am not "married" to this code so if you have an alternate suggestion to how I might do this fire away.

Kindness and thanks,

Dan

Daniel Elliott
  • 22,647
  • 10
  • 64
  • 82
  • Don't passing a byte[] into the X509 constructor try to parse the byte[] as a X509 certificate, is 'key' actually a certificate or are you trying to encrypt using an arbitrary string? Not sure if this might help: http://msdn.microsoft.com/en-us/library/ms180959.aspx – Lazarus Sep 23 '09 at 12:31
  • Thanks Laz, but yes it is a certificate read into a string – Daniel Elliott Sep 23 '09 at 12:33
  • I guess the other question is, can you decode the output from your encode routine? – Lazarus Sep 23 '09 at 12:39
  • No ... unfortunately ... therein lies my issue. – Daniel Elliott Sep 23 '09 at 12:42

2 Answers2

1

This line is the problem:

var av = new AlternateView(ms, "application/pkcs7-mime; smime-type=enveloped-data;name=smime.p7m; content-transfer-encoding=Base64; content-disposition=attachment; fileName=smime.p7m;");

You are dumping the values of multiple headers into the Content-Type header.

Instead, what you want is something more like this:

var contentType = new ContentType ("application/pkcs7-mime; smime-type=enveloped-data; name=smime.p7m");
var attachment = new Attachment (ms, contentType);
attachment.ContentDisposition.FileName = "smime.p7m";
attachment.TransferEncoding = TransferEncoding.Base64;

That said, I'm working on a far far far better MIME library than System.Net.Mail. This library is called MimeKit and I've already started working on S/MIME support.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
0

I think the default encoder/decoder for StreamReader is UTF-8. What happens if you change it to ASCII in the constructor (last line)?

liggett78
  • 11,260
  • 2
  • 29
  • 29