S/Mime 加密字符串

发布于 2024-08-05 18:44:18 字数 1104 浏览 9 评论 0原文

我的任务是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴我心暖 2024-08-12 18:44:18

这一行就是问题所在:

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;");

您正在将多个标头的值转储到 Content-Type 标头中。

相反,您想要的更像是这样的:

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;

也就是说,我正在开发一个比 System.Net.Mail 好得多的 MIME 库。这个库称为 MimeKit,我已经开始致力于 S/MIME 支持。

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.

蓝眼睛不忧郁 2024-08-12 18:44:18

我认为 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)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文