title: Encryption and Decryption in C# using MD5CryptoServiceProvider
body: <p>I have a service named CryptoService which inherit the interface ICryptoService that consist of 2 method Crypto Decrypto function. </p>
  
  <p>Encryption of a string is Working but the  Decryption of hash result will return my key instead of the Decrypted string.</p>
  
  <p>Here is my encryption code : </p>
  
  <pre><code>private string _salt = "*1234567890!@#$%^&amp;*()14344*";        
  private string Crypto(string text)
          {
              var hashmd5 = new MD5CryptoServiceProvider();
              byte[] toEncryptArray = Encoding.UTF8.GetBytes(_salt);
  
              byte[] keyArray = hashmd5.ComputeHash(Encoding.UTF8.GetBytes(_salt));
              hashmd5.Clear();
  
              TripleDesProvider.Key = keyArray;
              TripleDesProvider.Mode = CipherMode.ECB;
              TripleDesProvider.Padding = PaddingMode.PKCS7;
  
              ICryptoTransform cTransform = TripleDesProvider.CreateEncryptor();
  
              byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  
              return Convert.ToBase64String(resultArray, 0, resultArray.Length);
          }
  </code></pre>
  
  <p></p>
  
  <p>My decryption code : </p>
  
  <pre><code>private string Decrypto(string text)
          {
              try
              {
  
                  var hashmd5 = new MD5CryptoServiceProvider();
                  byte[] toEncryptArray = Convert.FromBase64String(text);
  
                  byte[] keyArray = hashmd5.ComputeHash(Encoding.UTF8.GetBytes(_salt));
  
                  hashmd5.Clear();
  
                  TripleDesProvider.Key = keyArray;
                  TripleDesProvider.Mode = CipherMode.ECB;
                  TripleDesProvider.Padding = PaddingMode.PKCS7;
  
                  ICryptoTransform cTransform = TripleDesProvider.CreateDecryptor();
                  byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  
                  TripleDesProvider.Clear();
  
                  return Encoding.UTF8.GetString(resultArray);
                  //return Encoding.UTF8.GetString(resultArray);
              }
              catch
              {
                  return string.Empty;
              }
          }
  </code></pre>
  
  <p></p>
  
  <p>provided here the screenshot in action.</p>
  
  <p><a href="https://i.stack.imgur.com/NWJvc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NWJvc.png" alt="enter image description here"></a></p>
  
  <p>here are the references of my codes I found in the internet 
  <a href="https://www.codeproject.com/Articles/14150/Encrypt-and-Decrypt-Data-with-C" rel="nofollow noreferrer">https://www.codeproject.com/Articles/14150/Encrypt-and-Decrypt-Data-with-C</a></p>