S/Mime 加密字符串
我的任务是使用 S/Mime 加密来加密字符串。很久以前,我工作的公司为此购买了一个组件(从 IPWorks),但为了让他们的组件在我们的服务器上正常运行,我们经历了无数的痛苦。不是功能问题,而是更多许可问题。
所以总而言之,我必须自己做。我查阅了 MSDN 和论坛并整理了以下代码。不幸的是,它创建的输出不是我所期望的。有很多我没想到的韩语和特殊字符。
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();
}
有人能看出这里有明显的错误吗?
我并没有与这段代码“结婚”,所以如果你对我如何解决这个问题有其他建议。
善意和感谢,
丹
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这一行就是问题所在:
您正在将多个标头的值转储到 Content-Type 标头中。
相反,您想要的更像是这样的:
也就是说,我正在开发一个比 System.Net.Mail 好得多的 MIME 库。这个库称为 MimeKit,我已经开始致力于 S/MIME 支持。
This line is the problem:
You are dumping the values of multiple headers into the Content-Type header.
Instead, what you want is something more like this:
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.
我认为 StreamReader 的默认编码器/解码器是 UTF-8。如果在构造函数(最后一行)中将其更改为 ASCII 会发生什么?
I think the default encoder/decoder for StreamReader is UTF-8. What happens if you change it to ASCII in the constructor (last line)?