title: Encrypting Data using RSA Public and Private Key in C#
body: <p>I am trying to encrypt a message using RSA public key and decrypt it using my private key. It encrypted the message, but I was not able to decrypt it.  The message was still encrypt after the final process.  When I exported the private key, it also included the public keys. I tried to remove the public key leaving, but it would not work.
  Here are the private and public keys</p>
  <pre><code>//This is the public key
  private const string public_key =  &quot;&lt;RSAKeyValue&gt;&lt;Modulus&gt;uznzVPilsR1rWPkpq6m6IALaafDnVZTDDcnEyBD3A/PBx2JZTKM0DTgiTDDwCEmQBNBpPILcIBdtg3aSUgicair+2ksYrVFT+uiy0Zy1nU6qoJ+SsapLKrpCa1zHpV4LMO/pFo4Foqzw0C1FNe56FXo1xj77GPgeYl0MHUVtAUc=&lt;/Modulus&gt;&lt;Exponent&gt;AQAB&lt;/Exponent&gt;&lt;/RSAKeyValue&gt;&quot;;
  
  //This is the private and public key.
  private const String private_key = &quot;&lt;RSAKeyValue&gt;&lt;Modulus&gt;uznzVPilsR1rWPkpq6m6IALaafDnVZTDDcnEyBD3A/PBx2JZTKM0DTgiTDDwCEmQBNBpPILcIBdtg3aSUgicair+2ksYrVFT+uiy0Zy1nU6qoJ+SsapLKrpCa1zHpV4LMO/pFo4Foqzw0C1FNe56FXo1xj77GPgeYl0MHUVtAUc=&lt;/Modulus&gt;&lt;Exponent&gt;AQAB&lt;/Exponent&gt;&lt;P&gt;+jPKs9JxpCSzNY+YNanz49Eo/A6RaU1DZWoFm/bawffZOompeL1jzpUlJUIrKVZJkNFvlxE90uXVwjxWBLv9BD==&lt;/P&gt;&lt;Q&gt;v5CVWKZ5Wo7W0QyoEOQS/OD8tkKS9DjzZnbnuo6lhcMaxsBrCWLstac1Xm2oFNtZgLtrPGbPfCNC5Su4Rz/P5w==&lt;/Q&gt;&lt;DP&gt;ZnyikmgobqEt20m3gnvcUDxT+nOJMsYYTklQhONoFj4M+EJ9bdy+Lle/gHSLM4KJ3c08VXgVh/bnSYnnfkb20Q==&lt;/DP&gt;&lt;DQ&gt;sSYGRfWk0W64Dpfyr7QKLxnr+Kv186zawU2CG44gWWNEVrnIAeUeWxnmi41CWw9BZH9sum2kv/pnuT/F6PWEzw==&lt;/DQ&gt;&lt;InverseQ&gt;XpWZQKXa1IXhF4FX3XRXVZGnIQP8YJFJlSiYx6YcdZF24Hg3+Et6CZ2/rowMFYVy+o999Y5HDC+4Qa1yWvW1vA==&lt;/InverseQ&gt;&lt;D&gt;Kkfb+8RrJqROKbma/3lE3xXNNQ7CL0F5CxQVrGcN8DxL9orvVdyjlJiopiwnCLgUHgIywceLjnO854Q/Zucq6ysm2ZRq36dpGLOao9eg+Qe8pYYO70oOkEe1HJCtP1Laq+f3YK7vCq7GkgvKAI9uzOd1vjQv7tIwTIADK19ObgE=&lt;/D&gt;&lt;/RSAKeyValue&gt;&quot;;
  
  //Encrypting the text using the public key
  private RSACryptoServiceProvider cipher = null;
  cipher = new RSACryptoServiceProvider();
              cipher.FromXmlString(public_key);
              byte[] data = Encoding.UTF8.GetBytes(txtUnencrypt.Text);
              byte[] cipherText = cipher.Encrypt(data, false);
              lblUnencryptMessage.Text = Convert.ToBase64String(cipherText);
  
          //    decryptText();
  
  //Trying to decrypt the text using the private key
  
  cipher = new RSACryptoServiceProvider();
              cipher.FromXmlString(private_key);
  
              byte[] ciphterText = Convert.FromBase64String(lblUnencryptMessage.Text);
              byte[] plainText = cipher.Decrypt(ciphterText, false);
              lblDecript.Text = Convert.ToBase64String(plainText);
  </code></pre>
  <p>For example, I encrypted the word &quot;Testing&quot;, the encrypted text was</p>
  <pre><code>kkqs+UGHNI7/3cKhQvSnJrKzNeCBQX9xHX2VrlyMvnwtszJAoFuViBZlfwmpVhqddnVUrlaqqkD7971E8L3wWltfGetK9nIljeo0GeietLYljoY0Gy3gatU++JPrqajAKxpIB75tvVlKXuYIs0qE3XWZu9bj0zAa4BVT2MhVNQM=&quot;
  </code></pre>
  <p>The decrypted text was</p>
  <pre><code>dGVzdGluZw==
  </code></pre>
  <p>What am I missing here?</p>