title: AES encrypt in Node.js  Decrypt in PHP.   Fail.
body: <p>In node.js, I use the build in function to encrypt data like that:</p>
  
  <pre><code>var text = "Yes";
  var password = "123456";
  var encrypt = crypto.createCipher('aes-256-cbc', password);
  var encryptOutput1 = encrypt.update(text, 'base64', 'base64');
  var encryptOutput2 = encrypt.final('base64');
  var encryptedText = encryptOutput1 + encryptOutput2;
  </code></pre>
  
  <p>the output (encrypted text) is: OnNINwXf6U8XmlgKJj48iA==</p>
  
  <p>Then I use decrypt it in PHP:</p>
  
  <pre><code>$encrypted = 'OnNINwXf6U8XmlgKJj48iA==';
  (or $encrypted = base64_decode('OnNINwXf6U8XmlgKJj48iA==')  );
  $dtext2 = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_CBC);
  echo "Decrypted: $dtext2";
  </code></pre>
  
  <p>I will get some funny characters, which I can't decrypted it.  I tried with/without base64_decode or MCRYPT_RIJNDAEL_128.. all fail.</p>
  
  <p>Then I check how the encryption in PHP, it looks very different from the output from node.js.</p>
  
  <pre><code>$text = "Yes";
      $key = "123456"; 
  
  
      $eText = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC);
      echo "Encrypted: $eText \n";
      echo "base64: " . base64_encode($eText) . " \n";
  
      $dtext1 = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $eText, MCRYPT_MODE_CBC);
      echo "Decrypted: $dtext1 \n\n";
  </code></pre>
  
  <p>It can encrypt and decrypt. and the encrypted data is : njCE/fk3pLD1/JfiQuyVa6w5H+Qb/utBIT3m7LAcetM=</p>
  
  <p>which is very different from the output from node.js   please advise how I can encrypt and decrypt between node.js &amp; php.  thanks. :)</p>
  
  <hr>
  
  <p>@Mel   here is what I have in PHP:</p>
  
  <pre><code>$text = "Yes";
  
  $key = "32BytesLongKey560123456789ABCDEF"; 
  $iv =  "sixteenbyteslong";
  
  /* Open the cipher */
  $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  
  /* Intialize encryption */
  mcrypt_generic_init($td, $key, $iv);
  
  /* Encrypt data */
  $eText = mcrypt_generic($td, $text);
  
  echo "Encrypted Data: $eText \n";
  echo "base64: " . base64_encode($eText) . " \n";
  
  /* Terminate encryption handler */
  mcrypt_generic_deinit($td);
  
  /* Initialize encryption module for decryption */
  mcrypt_generic_init($td, $key, $iv);
  
  /* Decrypt encrypted string */
  $dText = mdecrypt_generic($td, $eText);
  
  /* Terminate decryption handle and close module */
  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);
  
  /* Show string */
  echo trim($dText) . "\n";
  </code></pre>
  
  <p>However, it still doesn't work.</p>
  
  <p>The encrypted base 64 in PHP is: <strong>80022AGM4/4qQtiGU5oJDQ==</strong>
  The encrypted base 64 in nodejs is: <strong>EoYRm5SCK7EPe847CwkffQ==</strong></p>
  
  <p>thus, i can't decrypt the nodejs one in PHP. </p>
  
  <p>I wonder if it is because nodejs doesn't require $iv?   </p>