title: Decrypting Blowfish CBC in Javascript that has been encrypted with PHP mcrypt
body: <p>I'm having a hard time trying to decrypt a String that has been encrypted with Blowfish CBC in PHP;</p>
  
  <p>I'm working on the client side in Javascript with Node.js on a unit test written with Mocha.
  I'm using the inbuilt crypto functions of Node.</p>
  
  <p>Here's is the code that is working to decrypt the string in PHP.</p>
  
  <pre><code>&lt;?php
   $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
   $iv = "my_iv";
   $key = substr("my_key", 0, 56);
  
   // I read that php automaticly pads my key with \0 to make it 56char long.
  
   if (mcrypt_generic_init($cipher, $key, $iv) != -1) {
     $text = mdecrypt_generic($cipher, urldecode("X%10%8F%AD%1C%1D%E1f%29%CD%D2%E1%81%B0%BE%D2M%8E3%D6%5DO%29%E3%E6%EF%24%A0%E0g%22%DA%D7YK%5B%AA%E6P%91%BA%B8U%C1%87k%7F%07%FD%9D%BF%1F%BD%95r%04%F0%18%A9%27%E17%2C%A8"));
     mcrypt_generic_deinit($cipher);
     echo rtrim($text, "\0");
   } else {
      echo "ERRROR";
   }
  ?&gt;
  </code></pre>
  
  <p>This code give me the correct output, which is a serialised Array as a String.</p>
  
  <p>I tried then two libraries to decrypt using the same flow in JS,</p>
  
  <p>With crypto (inbuilt with node):</p>
  
  <pre><code>var crypto = require('crypto');
  var iv = new Buffer('my_iv');
  var key = new Buffer('my_key');
  var text = new Buffer(unescape("X%10%8F%AD%1C%1D%E1f%29%CD%D2%E1%81%B0%BE%D2M%8E3%D6%5DO%29%E3%E6%EF%24%A0%E0g%22%DA%D7YK%5B%AA%E6P%91%BA%B8U%C1%87k%7F%07%FD%9D%BF%1F%BD%95r%04%F0%18%A9%27%E17%2C%A8"));
  var decipher = crypto.createDecipheriv('bf-cbc', key, iv);
  var decrypted = decipher.update(text, '', '');
      decrypted += decipher.final('');
      console.log(decrypted);
  </code></pre>
  
  <p>Gives me this error :</p>
  
  <pre><code>error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
  TypeError: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
  at Decipheriv.Cipher.final (crypto.js:320:27)
  </code></pre>
  
  <p>And with the mcrypt module for node : <a href="https://github.com/tugrul/node-mcrypt" rel="nofollow">https://github.com/tugrul/node-mcrypt</a></p>
  
  <pre><code>    var MCrypt = require('mcrypt').MCrypt;
      var bfCcb = new MCrypt('blowfish', 'cbc');
      var iv = Buffer.from('my_iv');
      var key = Buffer.from('my_key_padded_with_0\0\0');
      var text = Buffer.from(unescape("X%10%8F%AD%1C%1D%E1f%29%CD%D2%E1%81%B0%BE%D2M%8E3%D6%5DO%29%E3%E6%EF%24%A0%E0g%22%DA%D7YK%5B%AA%E6P%91%BA%B8U%C1%87k%7F%07%FD%9D%BF%1F%BD%95r%04%F0%18%A9%27%E17%2C%A8"));
  
      bfCcb.open(key, iv);
  
      var plaintext = bfCcb.decrypt(text);
      console.log(plaintext.toString());
  </code></pre>
  
  <p>It gives me the wrong output,</p>
  
  <p>I don't know if it has something to do with possible encoding, or if i'm missing something with configuring my javascripts objects...</p>
  
  <p>If anyone has already encountered this problem, please let me know.</p>