C# 中使用 *.Pem 文件解密字符串
好的,我有一个以 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 thePrivateKey
property.See how to get private key from PEM file? for more details.