使用 openssl/makecert 工具创建 x509 证书

发布于 2024-09-05 03:58:26 字数 907 浏览 15 评论 0原文

我正在使用 makecert 使用以下参数创建 x509 证书:

makecert -r -pe -n "CN=Client" -ss MyApp

我想使用此证书通过 RSA 算法加密和解密数据。 我查看 Windows 证书存储中生成的证书,一切似乎都正常(它有一个私钥,公钥是一个 1024 位的 RSA 密钥,依此类推。)

现在我使用此 C# 代码来加密数据:

X509Store store = new X509Store("MyApp", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectName, "Client", false);
X509Certificate2 _x509 = certs[0];

using (RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)_x509.PublicKey.Key)
{
    byte[] dataToEncrypt = Encoding.UTF8.GetBytes("hello");
    _encryptedData = rsa.Encrypt(dataToEncrypt, true);
}

执行 Encrypt 方法时,我收到带有消息“错误密钥”的 CryptographicException。

我认为代码很好。可能我没有正确创建证书。 有什么意见吗? 谢谢

----------------编辑--------------
如果有人知道如何使用 OpenSsl 创建证书,这对我来说也是一个有效的答案。

I'm creating a x509 certificate using makecert with the following parameters:

makecert -r -pe -n "CN=Client" -ss MyApp

I want to use this certificate to encrypt and decrypt data with RSA algoritm.
I look to generated certificate in windows certificate store and everything seems ok (It has a private key, public key is a RSA key with 1024 bits and so on..)

Now i use this C# code to encrypt data:

X509Store store = new X509Store("MyApp", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectName, "Client", false);
X509Certificate2 _x509 = certs[0];

using (RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)_x509.PublicKey.Key)
{
    byte[] dataToEncrypt = Encoding.UTF8.GetBytes("hello");
    _encryptedData = rsa.Encrypt(dataToEncrypt, true);
}

When executing the Encrypt method, i receive a CryptographicException with message "Bad key".

I think the code is fine. Probably i'm not creating the certificate properly.
Any comments?
Thanks

---------------- EDIT --------------
If anyone know how to create the certificate using OpenSsl, its also a valid answer for me.

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

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

发布评论

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

评论(2

口干舌燥 2024-09-12 03:58:26

要允许密钥用于加密,您应该使用 -sky-选项。默认情况下,“makecert”使用 AT_SIGNATURE 密钥规范,该规范不适用于加密/解密。相反,通过发出以下命令来使用 AT_KEYEXCHANGE 规范:(

makecert -r -pe -n "CN=Client" -ss MyApp -sky Exchange

请记住删除以前的密钥或使用另一个容器名称)。

To allow the key to be used for encryption, you should use the -sky-option. Per default ´makecert` uses the AT_SIGNATURE key specification, which will not work with encryption/decryption. Instead have it use the AT_KEYEXCHANGE specification by issuing the following command:

makecert -r -pe -n "CN=Client" -ss MyApp -sky Exchange

(Remember to delete the previous key or use another container-name).

寄人书 2024-09-12 03:58:26

这是我在尝试使用 c# 查找 x509 证书和 rsa 的 makcert 用法示例时偶然发现的另一个页面,不幸的是它只提供了部分解决方案。我将所有内容放在人们可能感兴趣的博客条目中,可以在这里找到:
http://nick-howard.blogspot.com /2011/05/makecert-x509-certificates-and-rsa.html

This was another page I stumbled across when I was trying to find examples of makcert usage with x509 certificates and rsa using c#, and unfortunately it only provided part of the solution. I put all the bits together in a blog entry that people might be interested in, and it can be found here:
http://nick-howard.blogspot.com/2011/05/makecert-x509-certificates-and-rsa.html

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