title: How to decrypt the encrypted data from PHP using NodeJS Crypto Package
body: <p>I have created an encryption function in PHP using <code>openssl_encrypt()</code> with <code>aes-256-cbc</code> as algorithm. I want the encrypted data to be decrypted in NodeJS when sent in an <code>API</code> call.</p>
  
  <p>This is the encryption in my PHP:</p>
  
  <pre><code>$form_data_str = json_encode($form_data);
  
    // Encrypt data to submit
    define('AES_256_CBC', 'aes-256-cbc');
    // Encryption Key from Merchant sKey
    $sKey = 'uTEW2U0s90mtzF5nGX2BBKkuYcUsQEEK';
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(AES_256_CBC));
    $encrypted_form_data = openssl_encrypt($form_data_str, AES_256_CBC, $encryption_key, 0, $iv);
  </code></pre>
  
  <p>then I pass the encrypted data as body in an API call to my NodeJS server.
  Now, I want to decrypt the data in my NodeJS. I have this current code in my NodeJS:</p>
  
  <pre><code>var encrypter = require('crypto'),
  algorithm = 'aes-256-cbc',
  password = 'uTEW2U0s90mtzF5nGX2BBKkuYcUsQEEK';
  
  function decrypt(data){
      let iv = new Buffer.alloc(16);
      var decipher = encrypter.createDecipheriv(algorithm,password,iv)
      var decrypted = decipher.update(data,'hex','utf8')
      decrypted += decipher.final('utf8');
      return decrypted;
  }
  
  var decrypted = decrypt('encrypted data from request body');
  console.log(decrypted)
  </code></pre>
  
  <p>In <code>console</code> I have this error:</p>
  
  <pre><code>crypto.js:183
  
  
   var ret = this._handle.final();
                           ^
  
  Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length at Decipheriv.final (crypto.js:183:26)
  </code></pre>
  
  <p>What should be the correct approach in my <code>decrypt</code> function in NodeJS?</p>