title: Blowfish-encrypted messages between NSIS and PHP
body: <p>For a project I'm working on, I need to encrypt and decrypt a string using Blowfish in a compatible way across NSIS and PHP.</p>
  
  <p>At the moment I'm using the <a href="http://nsis.sourceforge.net/Blowfish%2B%2B_plug-in" rel="nofollow noreferrer">Blowfish++ plugin</a> for NSIS and the <code>mcrypt</code> library with PHP.  The problem is, I can't get them both to produce the same output.</p>
  
  <p>Let's start with the NSIS Blowfish++ plugin.  Basically the API is:</p>
  
  <pre><code>; Second argument needs to be base64 encoded
  ; base64_encode("12345678") == "MTIzNDU2Nzg="
  
  blowfish::encrypt "test@test.com***" "MTIzNDU2Nzg="
  Pop $0 ; 0 on success, 1 on failure
  Pop $1 ; encrypted message on success, error message on failure
  </code></pre>
  
  <p>There's no mention of whether it's CBC, ECB, CFB, etc. and I'm not familiar enough with Blowfish to be able to tell by  reading the <a href="http://nsis.sourceforge.net/File:Blowfish%2B%2B.zip" rel="nofollow noreferrer">mostly undocumented source</a>.  I assume it's ECB since the <a href="http://www.php.net/manual/en/function.mcrypt-generic-init.php" rel="nofollow noreferrer">PHP docs for <code>mcrypt</code> tells me that ECB doesn't need an IV</a>.</p>
  
  <p>I've also learned by reading the source code that the Blowfish++ plugin will Base64 decode the second argument to encrypt (I'm not sure why).  It also returns a Base64 encoded string.</p>
  
  <p>For the PHP side of things, I'm basically using this code to encrypt:</p>
  
  <pre><code>$plainText = "test@test.com***";
  $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_ECB, '');   
  $iv = '00000000';  // Show not be used anyway.
  $key = "12345678";
  
  $cipherText = "";
  if (mcrypt_generic_init($cipher, $key, $iv) != -1)
  {
      $cipherText = mcrypt_generic($cipher, $plainText);
      mcrypt_generic_deinit($cipher);        
  }
  
  echo base64_encode($cipherText);
  </code></pre>
  
  <p>However, if I do all these things, I get the following output from each:</p>
  
  <pre><code>NSIS: GyCyBcUE0s5gqVDshVUB8w==
  PHP:  BQdlPd19zEkX5KT9tnF8Ng==
  </code></pre>
  
  <p>What am I doing wrong?  Is the NSIS plugin not using ECB?  If not, what is it using for it's IV?</p>