title: How to import private key to c# RSAParameters object?
body: <p>I want to encrypt and decrypt data in c# with rsa. I wrote this code and it works fine for encrypting but i have problem with decrypting. What is my problem?!</p>
  
  <p>Here is my code:</p>
  
  <pre><code>using System;
  using System.Security.Cryptography;
  using System.IO;
  using System.Text;
  
  namespace Cry
  {
      public class CryptoUtils
      {
          public CryptoUtils ()
          {    
              UnicodeEncoding ByteConverter = new UnicodeEncoding();
              byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
              byte[] encryptedData;
              byte[] decryptedData;
              string decrptedStr = "";
  
              var pub = Convert.FromBase64String("MCgCIQCfkl4xV5T/v3r1bifOc1mVHa9yak5pGjUfAv0r+s6+AwIDAQAB");
              var prv = Convert.FromBase64String("MIGsAgEAAiEAn5JeMVeU/7969W4nznNZlR2vcmpOaRo1HwL9K/rOvgMCAwEAAQIg\nMce6pM/6xpIYrMoxluE7JBkVe9Sme9d6NPPJJX3NyBECEgCmwIarl1hSBnTqZNwJ\n8hZhqwIQAPT6CO/l/ma1sDi7eM7tCQISAKH90lYLlr9IinfSN3hp95g1AhAAlyNf\nuioqX1G+y/GVogyJAhEmQQB52juSQ574HnampzXUpQ==");
  
              using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
                  RSAParameters myRSAParameters = RSA.ExportParameters(false);
                  myRSAParameters.Modulus = pub;
                  myRSAParameters.Exponent = ByteConverter.GetBytes("65537");
                  myRSAParameters.D = prv;
                  encryptedData = RSAEncrypt(dataToEncrypt, myRSAParameters, false);
                  decryptedData = RSADecrypt(encryptedData, myRSAParameters, false);
                  decrptedStr = ByteConverter.GetString(decryptedData);
              }
          }
  
          static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) {
              try {
                  byte[] encryptedData;
                  using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
                      RSA.ImportParameters(RSAKeyInfo);
                      encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
                  }
                  return encryptedData;
              } catch (CryptographicException e) {
                  Console.WriteLine(e.Message);
  
                  return null;
              }
  
          }
  
          static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) {
              try {
                  byte[] decryptedData;
                  using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
                      RSA.ImportParameters(RSAKeyInfo);
                      decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
                  }
                  return decryptedData;
              } catch (CryptographicException e) {
                  Console.WriteLine(e.ToString());
  
                  return null;
              }
  
          }
      }
  }
  </code></pre>
  
  <p>In line <code>encryptedData = RSAEncrypt(dataToEncrypt, myRSAParameters, false);</code> data encrypts but in the next line i have this error:</p>
  
  <pre><code>System.Security.Cryptography.CryptographicException: PKCS1 decoding error.
    at System.Security.Cryptography.RSAPKCS1KeyExchangeDeformatter.DecryptKeyExchange (System.Byte[] rgbIn) [0x00000] in &lt;filename unknown&gt;:0 
    at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt (System.Byte[] rgb, Boolean fOAEP) [0x00000] in &lt;filename unknown&gt;:0
  </code></pre>
  
  <p>I think i did'nt fill <code>myRSAParameters</code> attributes correctly but i don't know how can fill it!</p>