如何在c#中使用bouncycastle将pem公钥转换为rsa公钥?

时间:2012-02-01 06:47:07

标签: c# rsa bouncycastle public-key pem

我有一个pem public 键,我想转换为xml格式的公钥或AsymmetricKeyParameter。

我可以将pem 私有键转换为公共/私有 xml格式,或者使用C#中的bouncyCastle中的PemReader将非对称密钥转换为。但是当使用Pem 公开时键入PemReader,我收到错误。

请帮助我。
我的问题还有什么解决方案?

2 个答案:

答案 0 :(得分:4)

这应该是你在寻找使用BouncyCastle的东西。

依赖关系:

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;

从PEM转换为RSA XML格式的代码:

StreamReader reader = new StreamReader("yourPrivateKey.pem");
PemReader pemReader = new PemReader(reader);
AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject();
AsymmetricKeyParameter privateKey = keyPair.Private;
RSA rsa = DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters) privateKey);
string xmlRsa = rsa.ToXmlString(true);
Console.WriteLine(xmlRsa);

答案 1 :(得分:2)

查看此条目from Microsoft forums,浏览至Bell_Wang回复,它指向一些为您进行转化的代码(code is here