title: RSA Encryption string public key to RSA Public Key
body: <p>The given public key to me is in this string format</p>
  <pre><code>string publicKey =
  &quot;MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlUCQZso6P43gKqw0CfTlwYb3N+m4v6IME 4nPA3WXe52wFpDM/JCFWSdXa7BewlwzDYjblgwL4u59CPxNTPTh7LTD4xXOaGDJHjX5+YgqK4fb9rs ImjMpIACrND/LAdrq5mctWWzw3UtW3F+o+sNwIZM8n65ysS+Vhq9IypFlfuQbWrKjAcWZ3u1iLtplz yf/pjhOEyyZiBUnh6D219+pMiE9nhCpc4xkH1gnlGszIDBqZMMULtGJvFXydA1vv5HxxCYJ2ydEzmA KYxVgA9BGXPEGE89dQbeJsieTj+FSsp9oTm+4vi345opRvH8DWhmZc4OPSwBEL8pwgS7cUnKPtwIDA QAB&quot;;
  </code></pre>
  <p>One line in the sample code in java that the public key needs to be converted to RSA Public key.
  Does the lines below converts a string public key to RSAPublicKey?  Is there a counterpart in C#?</p>
  <pre><code>byte[] decoedPublicKey = Base64.getDecoder().decode(publicKey);
  RSAPublicKey rsaPublicKey = (RSAPublicKey) KeyFactory.getInstance(&quot;RSA&quot;).
      generatePublic(new X509EncodedKeySpec(decoedPublicKey));
  </code></pre>
  <p>I am trying to send an RSA encrypted message to an external API and always the api returns an error that it could not decrypt the message.  I am not doing this step in my c# app, is this required in correct RSA encryption of the public key.  Below is a snippet of my RSA encryption. Is it correct?</p>
  <pre><code> Pkcs1Encoding pkcs1Encoding = new Pkcs1Encoding((IAsymmetricBlockCipher)new RsaEngine());
              using (StringReader reader = new StringReader(publicKey))
              {
                  AsymmetricKeyParameter parameters = (AsymmetricKeyParameter)new PemReader((TextReader)reader).ReadObject();
                  pkcs1Encoding.Init(true, (ICipherParameters)parameters);
              }
  
              return Convert.ToBase64String(pkcs1Encoding.ProcessBlock(bytes, 0, bytes.Length));
  </code></pre>