KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In node.js, I use the build in function to encrypt data like that: 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; the output (encrypted text) is: OnNINwXf6U8XmlgKJj48iA== Then I use decrypt it in PHP: $encrypted = 'OnNINwXf6U8XmlgKJj48iA=='; (or $encrypted = base64_decode('OnNINwXf6U8XmlgKJj48iA==') ); $dtext2 = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_CBC); echo "Decrypted: $dtext2"; I will get some funny characters, which I can't decrypted it. I tried with/without base64_decode or MCRYPT_RIJNDAEL_128.. all fail. Then I check how the encryption in PHP, it looks very different from the output from node.js. $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"; It can encrypt and decrypt. and the encrypted data is : njCE/fk3pLD1/JfiQuyVa6w5H+Qb/utBIT3m7LAcetM= which is very different from the output from node.js please advise how I can encrypt and decrypt between node.js & php. thanks. :) @Mel here is what I have in PHP: $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) . " \
Tags (comma-separated)
Save Edits
Cancel