Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AES Encrypt data in php and decrypt in node js.
//require cyrpto module
var crypto=require('crypto');
//key and iv should be same as the one in encrypt.php
var decipher=crypto.createDecipheriv('aes-256-cbc','12345678901234561234567890123456','1234567890123456');
//since we have already added padding while encrypting, we will set autopadding of node js to false.
decipher.setAutoPadding(false);
// copy the output of encrypt.php and paste it below
var cipherHexText256="a3c0d0b8a72c98f97dad01f0c0008c31ef9398e820ce520368ae2b24be844c78";
var dec = decipher.update(cipherHexText256,'hex','utf8');
//decrypted data is stored in dec
dec += decipher.final('utf8');
console.log(dec);
<?php
/*
creating cipher object using Rijndael encyrption algorithm with Cipher-block chaining (CBC) as mode of AES encryption
Here I have chosen 128 bit Rijndael encyrption
*/
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
/*
for 256 bit AES encryption key size should be of 32 bytes (256 bits)
for 128 bit AES encryption key size should be of 16 bytes (128 bits)
here i am doing 256-bit AES encryption
choose a strong key
*/
$key256 = '12345678901234561234567890123456';
/*
for 128 bit Rijndael encryption, initialization vector (iv) size should be 16 bytes
for 256 bit Rijndael encryption, initialization vector (iv) size should be 32 bytes
here I have chosen 128 bit Rijndael encyrption, so $iv size is 16 bytes
*/
$iv = '1234567890123456';
$plainText = 'This is plain text.';
mcrypt_generic_init($cipher, $key256, $iv);
// PHP pads with NULL bytes if $plainText is not a multiple of the block size
$cipherText256 = mcrypt_generic($cipher,$plainText );
mcrypt_generic_deinit($cipher);
/*
$cipherHexText256 stores encrypted text in hex
we will be decrypting data stored in $cipherHexText256 from node js
*/
$cipherHexText256 =bin2hex($cipherText256);
/*
echoing $cipherHexText256 (copy the output)
*/
echo $cipherHexText256;
?>
@jonas-db

This comment has been minimized.

Copy link

commented Apr 21, 2014

Any chance you can post the reverse? Decrypt in php and encrypt in nodejs?

Thanks!

@RamonL

This comment has been minimized.

Copy link

commented May 7, 2014

Hi jonas-db,

I've done the reverse for node.js portion:

/*
 * Encrypt
 */
var crypto=require('crypto');

var cipher = crypto.createCipheriv('aes-256-cbc', '12345678901234561234567890123456', '1234567890123456');

var encryptedVal = cipher.update('This is plain text.', 'utf8', 'hex');

encryptedVal += cipher.final('hex');

console.log('encryptedVal: ', encryptedVal);

This complements pradipchitrakar decrypt.js file above.

Reference:

http://nodejs.org/api/crypto.html

@achimkoellner

This comment has been minimized.

Copy link

commented Dec 15, 2015

This will not work with utf8 strings I changed "binary" to "utf8" and it worked well.

@dwalbeck

This comment has been minimized.

Copy link

commented Jan 29, 2016

Here's the reverse in PHP that was requested

$encrypted_str = 'ac33bc3d8e41865621382e44a452f20d'; // as an example
$iv = '1234567890123456';
$key256 = '12345678901234561234567890123456';

$limbo = hex2bin($decrypt);
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($cipher, $key256, $iv);
$decrypted = mdecrypt_generic($cipher, $limbo);
echo $decrypted;

@dwalbeck

This comment has been minimized.

Copy link

commented Jan 29, 2016

Sorry, you'll also need to do this:
trim($decrypted);

@subversivo58

This comment has been minimized.

Copy link

commented Sep 4, 2016

Node encrypt to node decrypt result: decrypted string + strange simbols ???

PHP encrypt to PHP decrypt fine works.

Watts remove strange simbols in Node?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.