how can i convert pem public key to rsa public key with bouncycastle in c#?

倾然丶 夕夏残阳落幕 提交于 2019-12-07 09:44:24

问题


i have a pem public key and i want to convert to xml format public key or AsymmetricKeyParameter.

i can convert pem Private key to Public/Private xml format or asymmetricKeyParameter with PemReader in bouncyCastle in C#.but when use Pem Public Key in PemReader , i receive error.

please help me.
what else solution for my problem?


回答1:


This should do what you were looking for using BouncyCastle.

Dependencies:

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

The code to convert from PEM to RSA XML format:

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);



回答2:


Take a look on this entry from Microsoft forums browse down to Bell_Wang reply, it points to some code that makes that conversion for you (code is here)



来源:https://stackoverflow.com/questions/9091900/how-can-i-convert-pem-public-key-to-rsa-public-key-with-bouncycastle-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!