阅读只有使用充气城堡PEM RSA公钥 [英] Reading PEM RSA Public Key Only using Bouncy Castle
问题描述
我想用C#中只包含一个RSA公钥的质子交换膜
文件中读取。我没有访问私有密钥的信息,也没有我的应用程序需要它。文件 myprivatekey.pem
文件以
I am trying to use C# to read in a .pem
file that contains only a RSA public key. I do not have access to the private key information, nor does my application require it. The file myprivatekey.pem
file begins with
----- BEGIN公钥-----
和
----- END PUBLIC KEY -----
结束。
我当前的代码如下:
Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair;
using (var reader = File.OpenText(@"c:\keys\myprivatekey.pem"))
keyPair = (Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair)new Org.BouncyCastle.OpenSsl.PemReader(reader).ReadObject();
然而,代码抛出 InvalidCastException的
通过消息
无法转换类型
的对象Org.BouncyCastle.Crypto.Parameters.DsaPublicKeyParameters键入
Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair。
Unable to cast object of type 'Org.BouncyCastle.Crypto.Parameters.DsaPublicKeyParameters' to type 'Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair'.
我如何使用充气城堡的 PemReader
时,没有私钥信息提供给只读公钥,?
How can I use Bouncy Castle's PemReader
to read only a public key, when no private key information is available?
推荐答案
下面的代码将读取公开关键只给一个文件名。异常处理应该改变任何生产代码。这个方法返回一个 AsymetricKeyParameter
。
The following code will read a public key only given a filename. The exception handling should be changed for any production code. This method returns an AsymetricKeyParameter
.
public Org.BouncyCastle.Crypto.AsymmetricKeyParameter ReadAsymmetricKeyParameter(string pemFilename)
{
var fileStream = System.IO.File.OpenText (pemFilename);
var pemReader = new Org.BouncyCastle.OpenSsl.PemReader (fileStream);
var KeyParameter = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pemReader.ReadObject ();
return KeyParameter;
}
这篇关于阅读只有使用充气城堡PEM RSA公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!