-
-
Save photoup-godwinh/1dc0284ee0bd1246f8b538c095ebeb74 to your computer and use it in GitHub Desktop.
AES Encrypt data in php and decrypt in node js.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment