title: RSA Encryption and Decryption with X509certificate2
body: <p>So, what I need is next:</p>
  
  <ol>
  <li>Create certifiactes for development, get one for the client and one for server</li>
  <li>Retrieve password through API that is encoded from client and decode it on server</li>
  </ol>
  
  <p>Now, I managed to create certifiactes following <a href="https://blog.jayway.com/2014/09/03/creating-self-signed-certificates-with-makecert-exe-for-development/" rel="nofollow noreferrer">this link</a>. The girl there gave step by step instructions on how to get self signed certifiactes, put them in store, etc... Now, the part I'm having problem with:</p>
  
  <p>I've managed to encrypt my data using this code:</p>
  
  <pre><code>public static string Encrypt(string stringForEncription, string PathToPrivateKey)
      {
          X509Certificate2 myCertificate;
          try
          {
              myCertificate = new X509Certificate2(PathToPrivateKey, "Test123");
          }
          catch (Exception e)
          {
              throw new CryptographicException("Unable to open key file.");
          }
  
          RSACryptoServiceProvider rsaObj;
          if (myCertificate.HasPrivateKey)
          {
              rsaObj = (RSACryptoServiceProvider)myCertificate.PrivateKey;
          }
          else
              throw new CryptographicException("Private key not contained within certificate.");
  
          if (rsaObj == null)
              return String.Empty;
  
          byte[] decryptedBytes;
          byte[] array = Encoding.UTF8.GetBytes(stringForEncription);
          try
          {
              decryptedBytes = rsaObj.Encrypt(array, false);
              //decryptedBytes = rsaObj.Encrypt(Convert.FromBase64String(Base64EncryptedData), false);
          }
          catch (Exception e)
          {
              throw new CryptographicException("Unable to encrypt data.");
          }
  
          //    Check to make sure we decrpyted the string 
          if (decryptedBytes.Length == 0)
              return String.Empty;
          else
              return System.Text.Encoding.UTF8.GetString(decryptedBytes);
      }
  </code></pre>
  
  <p>For PathToPrivate key variable I am using the path to my client ClientCert.pfx. I don't know if I should use any other, but here is the snap of the folder with all the certificates that I made:</p>
  
  <p><a href="https://i.stack.imgur.com/jcQHg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jcQHg.png" alt="enter image description here"></a></p>
  
  <p>Now, for the decryption, I'm using next code:</p>
  
  <pre><code> public static string DecryptEncryptedData(string Base64EncryptedData, string PathToPrivateKey)
      {
          X509Certificate2 myCertificate;
          try
          {
              myCertificate = new X509Certificate2(PathToPrivateKey, "Test123");
          }
          catch (Exception e)
          {
              throw new CryptographicException("Unable to open key file.");
          }
  
          RSACryptoServiceProvider rsaObj;
          if (myCertificate.HasPrivateKey)
          {
              rsaObj = (RSACryptoServiceProvider)myCertificate.PrivateKey;
          }
          else
              throw new CryptographicException("Private key not contained within certificate.");
  
          if (rsaObj == null)
              return String.Empty;
  
          byte[] decryptedBytes;
          try
          {
              decryptedBytes = rsaObj.Decrypt(Convert.FromBase64String(Base64EncryptedData), false);
          }
          catch (Exception e)
          {
              throw new CryptographicException("Unable to decrypt data.");
          }
  
          //    Check to make sure we decrpyted the string 
          if (decryptedBytes.Length == 0)
              return String.Empty;
          else
              return System.Text.Encoding.UTF8.GetString(decryptedBytes);
      }
  </code></pre>
  
  <p>And whatever I try to do, it gives me exception:</p>
  
  <pre><code>{"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. "}
  </code></pre>
  
  <p>Would really apreciate somebody helping me out.</p>