gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:21:34 26 4
gpt4 key购买 nike

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

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



我想使用此证书通过 RSA 算法加密和解密数据。
我在 Windows 证书存储中查看生成的证书,一切看起来都很好(它有一个私钥,公钥是一个 1024 位的 RSA key 等等..)

现在我使用这个 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 方法时,我收到带有消息“Bad key”的 CryptographicException。

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

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

最佳答案

要允许将 key 用于加密,您应该使用 -sky -option .默认“makecert”使用 AT_SIGNATURE key 规范,该规范不适用于加密/解密。而是通过发出以下命令使用 AT_KEYEXCHANGE 规范:

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

(记得删除以前的键或使用另一个容器名称)。

关于security - 使用 openssl/makecert 工具创建 x509 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3008163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com