[英]How do I read RSA public key from PEM file and use it to encrypt in BouncyCastle in C#?
The C# BouncyCastle contains a class called Org.BouncyCastle.Utilities.IO.Pem.PemReader that seem to take the RSA public key file in PEM format. C#BouncyCastle包含一个名为Org.BouncyCastle.Utilities.IO.Pem.PemReader的类,该类似乎采用PEM格式的RSA公钥文件。 I looked at this link: how can i convert pem public key to rsa public key with bouncycastle in c#?
我看了一下此链接: 如何在C#中使用Bouncycastle将Pem公钥转换为rsa公钥?
But it seemed to be using non-existent method on PemReader called ReadObject. 但是它似乎在PemReader上使用了不存在的称为ReadObject的方法。 So I wrote following code instead.
所以我改写了以下代码。
var pemReader = new PemReader(File.OpenText(@"...rsa public key file path ..."));
var pemObject = pemReader.ReadPemObject();
var rsaPublicKeyBytes = pemObject.Content;
Once I get the RSA public bytes, I am not sure how to proceed further. 一旦获得了RSA公共字节,就不确定如何继续进行操作。 I want to be able to do following:
我希望能够做到以下几点:
var rsaCipher = new RsaEngine();
var oaepEncoding = new OaepEncoding(rsaCipher, new Sha256Digest());
var publicKey = new RsaKeyParameters(...);
oaepEncoding.Init(true, publicKey);
var actualEncryptedBytes = oaepEncoding.ProcessBlock(plainBytes, 0, plainBytes.Length);
I guess I am not sure about how to construct RsaKeyParameters with RSA public bytes. 我想我不确定如何使用RSA公共字节构造RsaKeyParameters。 Can someone point me in the right direction?
有人可以指出我正确的方向吗? Or am I totally going the wrong way here?
还是我在这里完全走错路了?
You're using the wrong PemReader
, you want the one from Org.BouncyCastle.OpenSsl
. 您使用了错误的
PemReader
,您想要的是Org.BouncyCastle.OpenSsl
。
EDIT: For some reason OP is insistent that this class has no ReadObject
method. 编辑:由于某种原因,OP坚持认为此类没有
ReadObject
方法。 It does, and it can be seen here . 确实如此,可以在这里看到。
Like this: 像这样:
using System;
using System.IO;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
namespace ScratchPad
{
class MainClass
{
public static void Main(string[] args)
{
var pemReader = new PemReader(File.OpenText(@"/Users/horton/tmp/key-examples/myserver_pub.pem"));
var pemObject = (Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters)pemReader.ReadObject();
var rsa = DotNetUtilities.ToRSA(pemObject);
// ... more stuff ...
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.