title: PHP Triple DES encryption and compatible C# decryption
body: <p>I am decrypting a message in C#, this message is encrypted in PHP using below code:-</p>
  
  <pre><code>public function __construct($sEncryptionKey)
  {
      $this-&gt;link = mcrypt_module_open('tripledes', '', 'ecb', '');
      $this-&gt;sInitializationVector = mcrypt_create_iv(mcrypt_enc_get_iv_size($this-&gt;link), MCRYPT_RAND);
      $this-&gt;iKeySize = mcrypt_enc_get_key_size($this-&gt;link);
      $sEncryptionKey = substr(md5($sEncryptionKey), 0, $this-&gt;iKeySize);
      mcrypt_generic_init($this-&gt;link, $sEncryptionKey, $this-&gt;sInitializationVector);
  }
  
  public function encrypt($sDataToEncrypt)
  {
      return base64_encode(mcrypt_generic($this-&gt;link, $sDataToEncrypt));
  }
  </code></pre>
  
  <p>And I am using below decryption function in c# for decryption:-</p>
  
  <pre><code>public string Decrypt(string toDecrypt, string key, bool useHashing)
  {                      
      byte[] keyArray;
      byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);    
      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.Zeros;
  
      ICryptoTransform cTransform = tdes.CreateDecryptor();
      byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);                              
      tdes.Clear();
      var strValue = UTF8Encoding.UTF8.GetString(resultArray);    
      return UTF8Encoding.UTF8.GetString(resultArray);
  }
  </code></pre>
  
  <p>I tried with few changes and getting below results:-</p>
  
  <p>1.) PaddingMode.PKCS7 with no hashing  = "Specified key is not a valid size for this algorithm."</p>
  
  <p>2.) PaddingMode.PKCS7 with hashing =  "Bad Data."</p>
  
  <p>3.) PaddingMode.Zeros with no hashing = "Specified key is not a valid size for this algorithm."</p>
  
  <p>4.) PaddingMode.Zeros with hashing = "�8�f����q6IGs�" some unknown characters</p>
  
  <p>I think 4'th one will work, but not sure what I am doing wrong.</p>