title: Using MCrypt to Decrypt a Ciphertext (3DES CBC Mode)
body: <p>I have a ciphertext, encrypted in 3DES CBC mode, and I'm having some trouble decrypting it. I have the following information (not actual values):</p>
  
  <pre><code>&lt;?php
  // Three 16 character long keys
  $key1 = '1234567890123456';
  $key2 = '6543210987654321';
  $key3 = '6549873210456123';
  
  // The initialisation vector
  $ivec = '0102030405060708';
  
  // A token which looks something like this
  $token = '2ad82e09e3b4e7c491e4bb9fb7c606e1';
  ?&gt;
  </code></pre>
  
  <p>I based my decryption function off the PHP Class that is found in <a href="http://www.t4vn.net/tutorials/showtutorials/An-Introduction-to-Mcrypt-and-PHP.html" rel="nofollow noreferrer">this blog post</a>. It looks like this:</p>
  
  <pre><code>&lt;?php
  function decrypt($key,$iv,$token) {
      $td = mcrypt_module_open('tripledes', '', 'cbc', ''); 
      $iv = substr($iv, 0, mcrypt_enc_get_iv_size($td)); 
      $expected_key_size = mcrypt_enc_get_key_size($td); 
  
      $key = substr(md5($key), 0, $expected_key_size); 
      mcrypt_generic_init($td, $key, $iv);
  
      $decrypted_string = trim(mdecrypt_generic($td, base64_decode($token)));
  
      mcrypt_generic_deinit($td); 
      mcrypt_module_close($td); 
  
      return $decrypted_string;
  }
  ?&gt;
  </code></pre>
  
  <p>However, I'm not sure how to feed the three keys into the algorithm. I have tried chaining them together like so: $key1.$key2.$key3, and in reverse order, but to no avail.</p>
  
  <p>Any help would be appreciated.</p>