2012-02-01 162 views
3

我有一个朋友公众键,我想转换为XML格式的公钥或AsymmetricKeyParameter。我如何将pem公钥转换为rsa公钥与c#中的bouncycastle?

我可以私人密钥转换PEM到公共/私人 XML格式或asymmetricKeyParameter与PemReader在BouncyCastle的在C#。但在使用时的Pem 公共键入PemReader,我收到错误。

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

+0

有人知道这个问题? – 2012-02-01 09:59:16

+1

你有没有尝试使用openssl来做转换? – 2012-02-08 18:49:21

+0

什么是错误? – 2012-02-10 20:06:00

回答

2

这应该做你正在寻找使用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);