Length of the data to decrypt is invalid in md5 for C# windows application
<p>anybody can helpme on decryption error .iam facing "Length of the data to decrypt is invalid" this kind of error in the line</p>

<pre><code>byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length-1);
</code></pre>

<p>iam getting this encrypted value  from  sqldatabase and before showing in grid view iwant to decrypt the encryted value(this value coming from sqldb ). it is possible getting encrypted value from database .
if iam passing in right way why it is showing this error .the encryptin process doing well but the decryption process making this problem
        My code is see below ... Plz helpme
        iam selcting options from combobox and date time picker here iam using my own dataaccess layer for conect.</p>

<pre><code>       private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
           ReteriveGrid(comboBox1.SelectedIndex);      
       }
       private void ReteriveGrid(int istatus)
       {
           DataSet ds = new DataSet();
           IDBManager dbManager = new DBManager(DataProvider.SqlServer);
           //SqlConnection con = new SqlConnection("connectionstyring");
          dbManager.ConnectionString = ConfigurationSettings.AppSettings["SQLCN"].ToString();

           dbManager.Open();
           dbManager.CreateParameters(2);
           dbManager.AddParameters(0, "@istatus", istatus);
           dbManager.AddParameters(1, "@date", dateTimePicker1.Value.Date);

           ds = dbManager.ExecuteDataSet(CommandType.StoredProcedure, "sp_Getdata");
           for (int j = 0; j &lt; ds.Tables[0].Rows.Count; j++)
           {
               string DecryptText = ds.Tables[0].Rows[j][3].ToString().Trim();
               MessageBox.Show(DecryptText);
           //Here Call The DecryptionMethod
               string AfterDecrypText = Decrypt(DecryptText,true);
               MessageBox.Show(AfterDecrypText);
               ds.AcceptChanges();
           }
         dataGridView1.DataSource = ds;
       }
public static string Decrypt(string cipherString, bool useHashing)
      {
          byte[] keyArray;
          byte[] toEncryptArray = Convert.FromBase64String(cipherString);
          System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
          //Get your key from config file to open the lock!(string)settingsReader.GetValue("SecurityKey", typeof(String));
          string key = "Trms";
          if (useHashing)
          {
              MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
              keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
              hashmd5.Clear();
          }
          else
              keyArray = UTF8Encoding.UTF8.GetBytes(key);
          TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
          tdes.Key = keyArray;
          tdes.Mode = CipherMode.ECB;
          tdes.Padding = PaddingMode.PKCS7;
          ICryptoTransform cTransform = tdes.CreateDecryptor();

          byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length-1);
          tdes.Clear();
          return UTF8Encoding.UTF8.GetString(resultArray);
      }
</code></pre>

<p>ReplyQuote</p>
