如何使用 Mono Framework 和 BouncyCastle API 生成 CMS(加密消息语法)?

发布于 2024-10-20 09:18:50 字数 1030 浏览 7 评论 0原文

我有 PKCS#12 格式的证书,我需要生成 CMS 签名(加密消息语法)。由于“Mono Framework”没有完整实现的“System.Security.Cryptography”程序集,我尝试使用“Bouncy Castle API for C#”。

因此,使用“Bouncy Castle”,我需要编写一种替代代码来代替我在 DOTNET 上编写的代码。

DOT NET 上的代码如下:

X509Certificate2 crt = new X509Certificate2();

byte[] crtBytes = [ certificate in the format PKCS12 (certificate + private key) obtained using FileStream class]

crt.Import(crtBytes, "123456", X509KeyStorageFlags.DefaultKeySet);

Encoding msgCodificado = Encoding.UTF8;

byte[] msgBytes = msgCodificado.GetBytes(xmlTRA.OuterXml); // xmlTRA.OuterXml is the data to sign

ContentInfo pkcsContentInfo = new ContentInfo(msgBytes);

SignedCms cms = new SignedCms(pkcsContentInfo);
CmsSigner firmante = new CmsSigner(crt);

firmante.IncludeOption = X509IncludeOption.EndCertOnly;
cms.ComputeSignature(firmante); // ---> throw an cryptografy exception with MONO

byte[] firma = cms.Encode();
firmaB64 = Convert.ToBase64String(firma);

有人知道如何使用“Bouncy Castle API for C#”编写替代代码吗?

I have a Certificate in PKCS#12 format and I need to generate a CMS signature (Cryptographic Message Syntax). Due that "Mono Framework" does not have a full implemented "System.Security.Cryptography" assembly, I am trying to use "Bouncy Castle API for C#".

So, using "Bouncy Castle", I need to write an alternative code to the one I had wrote on DOTNET.

The code on DOT NET is the following:

X509Certificate2 crt = new X509Certificate2();

byte[] crtBytes = [ certificate in the format PKCS12 (certificate + private key) obtained using FileStream class]

crt.Import(crtBytes, "123456", X509KeyStorageFlags.DefaultKeySet);

Encoding msgCodificado = Encoding.UTF8;

byte[] msgBytes = msgCodificado.GetBytes(xmlTRA.OuterXml); // xmlTRA.OuterXml is the data to sign

ContentInfo pkcsContentInfo = new ContentInfo(msgBytes);

SignedCms cms = new SignedCms(pkcsContentInfo);
CmsSigner firmante = new CmsSigner(crt);

firmante.IncludeOption = X509IncludeOption.EndCertOnly;
cms.ComputeSignature(firmante); // ---> throw an cryptografy exception with MONO

byte[] firma = cms.Encode();
firmaB64 = Convert.ToBase64String(firma);

Anyone knows how to write an alternative code using "Bouncy Castle API for C#"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

掐死时间 2024-10-27 09:18:50

Org.BouncyCastle.Pkcs 具有用于使用 PKCS#12 存储的类。
Org.BouncyCastle.Cms 具有用于处理 CMS 消息的类。

源代码中有相应的测试类,展示了使用的基础知识,例如 Pkcs12Store(Builder) 和 CmsSignedData(Generator)。

Org.BouncyCastle.Pkcs has classes for working with a PKCS#12 store.
Org.BouncyCastle.Cms has classes for working with CMS messages.

There are corresponding test classes in the source code that show the basics of using, e.g. Pkcs12Store(Builder) and CmsSignedData(Generator).

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