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?

Convert RSA public key, from XML to PEM (PHP)

How convert RSA public key, from XML to PEM (PHP)?



How can I create a RSA public key in PEM format from an RSA modulus?

I have the modulus of an RSA public key. I want to use this public key with the Python library M2Crypto, but it requires a public key in PEM format. Thus, I have to convert the RSA modulus to a PEM

Convert RSA Public Key to PEM Format

I want to convert – RSA Public Key modulus: 9699c3c4406464638d2b30dbed44ddee485b5f9a3d7491434049440d34eb1759376a8bac0e37cee5c18df69acfc60d7252634fd15c26ab2afa16ca831598381356209acea9cea9467acdbd2a9b6d

Can anyone give me an example of using BouncyCastle to import .pem public DSA key into c#?

I am trying co import a .pem key into c#, and I’ve found a library, which does that: BouncyCastle I’ve created a code, which loads public key and is supposed to load the data into DSACryptoServiceProv

Convert RSA public key to RSA DER

I have id_rsa.pub key generated by ssh-keygen. How can I programmatically convert id_rsa.pub files to RSA DER formatted keys?

Reading PEM RSA Public Key Only using Bouncy Castle

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 fil

RSA key convert to PEM file

How can I convert this RSA public key: 109120132967399429278860960508995541528237502902798129123468757937266291492576446330739696001110 6039072308886100726558188253585034290 57592827629436413108566029

Export RSA public key to PEM String using java

So I’m using Spongy Castle (Android) to generate a PEM encoded string for an RSA public key that will be uploaded to a server. This is what I’m currently doing: PublicKey publicKey = keyPair.getPubli

How to read a RSA public key in PEM + PKCS#1 format

I have a RSA public key in PEM format + PKCS#1(I guess): —–BEGIN RSA PUBLIC KEY—– MIGJAoGBAJNrHWRFgWLqgzSmLBq2G89exgi/Jk1NWhbFB9gHc9MLORmP3BOCJS9k onzT/+Dk1hdZf00JGgZeuJGoXK9PX3CIKQKRQRHpi5e1vmO

How to generate the PEM serialization for the public RSA/DSA key

Using PyCrypto I was able to generate the public and private PEM serialization for a RSA key, but in PyCrypto the DSA class has no exportKey() method. Trying PyOpenSSL I was able to generate the priva

Answers

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)

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