使用2个证书加密文件

时间:2018-01-19 11:50:42

标签: c# encryption cryptography aes smime

通常我们使用opennssl加密文件。这就是我们这样做的方式。

openssl smime -encrypt -binary -aes256 -in file.cfg -out file.cfg.enc -outform der cert1 cert2

有人可以给我一个提示,请在c#中怎么做?

谢谢。

1 个答案:

答案 0 :(得分:1)

您需要EnvelopedCms,这可能需要您添加对System.Security.dll的引用。

ContentInfo content = new ContentInfo(File.ReadAllBytes("file.cfg"));

CmsRecipientCollection recipients = new CmsRecipientCollection
{
    new CmsRecipient(new X509Certificate2("cert1")),
    new CmsRecipient(new X509Certificate2("cert2")),
};

EnvelopedCms envelopedCms = new EnvelopedCms(
    content,
    new AlgorithmIdentifier(new Oid("2.16.840.1.101.3.4.1.42")));

envelopedCms.Encrypt(recipients);
File.WriteAllBytes("file.cfg.enc", envelopedCms.Encode());