title: Bad key/Key does not exist exception in RSA
body: <p>I am writing a program to encrypt and decrypt using the private and public keys generated from this website - <a href="http://www.mobilefish.com/services/rsa_key_generation/rsa_key_generation.php" rel="nofollow">rsa_key_generation</a></p>
  
  <p>I set the keysize to 512 and base64. And here is my code.</p>
  
  <pre><code>class Program
  {
      private static RSACryptoServiceProvider _rsaProvider = new RSACryptoServiceProvider();
      static string p = "DYy2r60MEQJR2gyYmkuEQp7F8a2D=rOjd60tOR=FF8Z";
      static string q = "8_oSxChqnGrGHTW0gt4CggZ2pOQZI8SR1uFWZIn4yRr";
      static string mod = "1vg1y6ZmYtGILIVgbgezzwUcnnmSRp1hwQuxRTIiQNbrz=fi4myRn7KwJyG2aKDEG69pseLMqetYYCTQUKCyr=";
      static string exp = "G01";
      static string d = "1khYtBuSMaCtYf7ECrIV6=b36bBGwxUvzTUrwQvRJNf70h0sFNnxBARiNrZdb8DWJsgjQEKhBUcWOD4aII1xv1";
      static string dP = "6VTNXCrpUJP_tbJbzCA_s38haB_T3IEGWYC_1pCpha=";
      static string dQ = "3=p7GQ9v613wldg8eSEdxNCvRfeIfBvhomPdigmMwhP";
  
      static void Main(string[] args)
      {
  
          string encryptedPIN = RSAEncrypt("1234");
          Console.WriteLine("Encrypted Text Recieved: " + encryptedPIN);
          string decryptedPIN = RSADecrypt(encryptedPIN);
          Console.WriteLine("\nDecrypted Text Recieved: " + decryptedPIN);
          Console.ReadLine();
      }
  
      public static string RSAEncrypt(string plainText)
      {
          AssingParams();
  
          RSAParameters rsaParam = new RSAParameters();
  
          rsaParam.Modulus = Encoding.UTF8.GetBytes(mod);
          rsaParam.Exponent = Encoding.UTF8.GetBytes(exp);            
  
          _rsaProvider.ImportParameters(rsaParam);
  
          byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
          byte[] cipherBytes = _rsaProvider.Encrypt(plainBytes, false);
          return Convert.ToBase64String(cipherBytes);
      }
  
      public static string RSADecrypt(string cipherText)
      {
          AssingParams();
          byte[] cipherBytes = Convert.FromBase64String(cipherText);
          RSAParameters rsaParam = new RSAParameters();
  
          rsaParam.Modulus = Encoding.UTF8.GetBytes(mod);
          rsaParam.D = Encoding.UTF8.GetBytes(d);
          rsaParam.Exponent = Encoding.UTF8.GetBytes(exp);
          rsaParam.Q = Encoding.UTF8.GetBytes(q);
          rsaParam.DP = Encoding.UTF8.GetBytes(dP);
          rsaParam.DQ = Encoding.UTF8.GetBytes(dQ);
  
          _rsaProvider.ImportParameters(rsaParam);
  
          byte[] plainText = _rsaProvider.Decrypt(cipherBytes, false);
          return Encoding.UTF8.GetString(plainText);
      }
  
      public static void AssingParams()
      {
          const int PROVIDER_RSA_FULL = 1;
          const string CONTAINER_NAME = "RSAContainer";
          CspParameters cspParams;
          cspParams = new CspParameters(PROVIDER_RSA_FULL);
          cspParams.KeyContainerName = CONTAINER_NAME;
          cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
          _rsaProvider = new RSACryptoServiceProvider(1024, cspParams);
      }       
  }
  </code></pre>
  
  <p>The variables declared as string p, q, mod, exp, d, dP and dQ are copied from the website. While the encryption works fine in decryption I get this error 'Bad Data'. I tried adding modulus to the decryption key then I get the error 'Key does not exist'.</p>
  
  <p>Please help.</p>