简体   繁体   中英

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

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?

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

看看微软论坛中的这个条目浏览到Bell_Wang的回复,它指向一些为你进行转换的代码代码在这里

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM