title: Encrypt and Decrypt password
body: <p>I am using the following code to encrypt and decrypt passwords in asp.net. the encryption works perfectly but when decrypting it throws this error <strong><code>Invalid length for a Base-64 char array.</code></strong></p>
  
  <p>my password is 123 and i am using it as follow to encrypt: <code>HttpUtility.UrlEncode(CryptorEngine.Encrypt(strpassword, true));</code>
  and to decrypt I am using it as follow :</p>
  
  <pre><code>CryptorEngine.Decrypt(HttpUtility.UrlDecode(strpassword), true));
  </code></pre>
  
  <p>and here is the code :</p>
  
  <pre><code> public class CryptorEngine
      {
          /// &lt;summary&gt;
          /// Encrypt a string using dual encryption method. Return a encrypted cipher Text
          /// &lt;/summary&gt;
          /// &lt;param name="toEncrypt"&gt;string to be encrypted&lt;/param&gt;
          /// &lt;param name="useHashing"&gt;use hashing? send to for extra secirity&lt;/param&gt;
          /// &lt;returns&gt;&lt;/returns&gt;
          public static string Encrypt(string toEncrypt, bool useHashing)
          {
              byte[] keyArray;
              byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
  
              System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
              // Get the key from config file
              string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
              //System.Windows.Forms.MessageBox.Show(key);
              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.CreateEncryptor();
              byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
              tdes.Clear();
              return Convert.ToBase64String(resultArray, 0, resultArray.Length);
          }
          /// &lt;summary&gt;
          /// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
          /// &lt;/summary&gt;
          /// &lt;param name="cipherString"&gt;encrypted string&lt;/param&gt;
          /// &lt;param name="useHashing"&gt;Did you use hashing to encrypt this data? pass true is yes&lt;/param&gt;
          /// &lt;returns&gt;&lt;/returns&gt;
          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 key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
  
              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);
  
              tdes.Clear();
              return UTF8Encoding.UTF8.GetString(resultArray);
          }
      }
  </code></pre>