title: Encrypt string in PHP and decrypt in Node.js
body: <p>I am sending data through insecure connection between Apache and Node.js servers. I need to encrypt data in PHP and decrypt in Node.js. I've spent 2 days trying to get it to work, however I only managed to get message signing to work, no encryption. I tried passing AES128-CBC, AES256-CBC, DES, AES128, AES256 as algorithms, however nothing worked well..</p>
  
  <p>I tried this in PHP:</p>
  
  <pre><code>$data = json_encode(Array('mk' =&gt; $_SESSION['key'], 'algorithm' =&gt; 'SHA1', 'username' =&gt; $_SESSION['userid'], 'expires' =&gt; $expires));
  $payload = openssl_encrypt($data, 'des', '716c26ef');
  return base64_encode($payload);
  </code></pre>
  
  <p>And in Node.js:</p>
  
  <pre><code>var enc_json = new Buffer(response[1], 'base64');
  var decipher = crypto.createDecipher('des', '716c26ef');
  var json = decipher.update(enc_json).toString('ascii');
  json += decipher.final('ascii');
  </code></pre>
  
  <p>And besides wrong decrypted data I get error such as these:</p>
  
  <pre><code>TypeError: error:0606508A:digital envelope routines:EVP_DecryptFinal_ex:data not multiple of block length
  
  TypeError: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
  </code></pre>
  
  <p>I need a simple encryption as data is not too sensitive (no password or user data), however data should only be read by the recipient. Key length can be anything, but procedure to encrypt/decrypt has to be as simple as possible, please no IVs.</p>