C# 中使用 *.Pem 文件解密字符串

发布于 2024-12-22 13:21:53 字数 579 浏览 3 评论 0原文

好的,我有一个以 Base64 编码的文本字符串。

我想将其从 Base64 解码为字节数组,然后用我的私钥解密。我的私钥是 *.pem 文件。我迷路了!

我想我需要声明一个字节数组,抓取 *.pem 的 ---BEGIN--- 和 ---END--- 部分之间的文本,并将其从 Base 64 字符串转换为我的结果字节数组。

然后,我需要声明一个 X509Certificate2,并使用采用字节数组和文本字符串的构造函数,字节数组是我的私钥,文本字符串是我的密码,如下所示:

byte[] myprivateKey = Convert.FromBase64String("BASE 64 ENCODED PRIVATE KEY GOES HERE");
X509Certificate2 myPem = new X509Certificate2(myprivateKey, "MY PASSPHRASE");

但是,我在以下位置收到以下错误这一点:

找不到请求的对象。

至少我正朝着正确的方向前进,还是偏离了方向?我需要在这里做什么?

Ok, I have a string of text, encoded in Base64.

I want to decode this from Base64 to a byte array, then decrypt this with my private key. My private key is a *.pem file. I am lost!

I think I need to declare a byte array, grab the text between the ---BEGIN--- and ---END--- part of my *.pem, and convert this from a base 64 string as the result of my byte array.

I then need to declare an X509Certificate2, and use the constructor that takes a byte array and a string of text, the byte array being my private key, the string of text being my passphrase like below:

byte[] myprivateKey = Convert.FromBase64String("BASE 64 ENCODED PRIVATE KEY GOES HERE");
X509Certificate2 myPem = new X509Certificate2(myprivateKey, "MY PASSPHRASE");

However, I am getting the following error at this point:

Cannot find the requested object.

Am I heading in the right direction at least or am I way off? What do I need to do here?

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

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

发布评论

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

评论(1

鹿港巷口少年归 2024-12-29 13:21:53

X509Certificate2 不会从 PEM base64 编码文件中读取私钥。您需要读取证书之外的私钥,然后将其分配给 PrivateKey 属性。

请参阅如何从 PEM 文件获取私钥?了解更多信息细节。

X509Certificate2 won't read a private key from a PEM base64-encoded file. You'll need to read the private key apart from the certificate and then assign it the the PrivateKey property.

See how to get private key from PEM file? for more details.

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