first of all: MCRYPT_RIJNDAEL_256 NOT (!) AES-256-CBC, if you want to use this encryption, you must use MCRYPT_RIJNDAEL_128 with 265-bit character with 32 characters.
This will be part of php:
function decrypt($data, $key) { if(32 !== strlen($key)) $key= hash('SHA256', $key, true); $data = base64_decode($data); $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, str_repeat("\0", 16)); $padding = ord($data[strlen($data) - 1]); return substr($data, 0, -$padding); }
This php function includes an add-on, which is an important part, because if the data length of the attached data is not a multiple of the key, you will get something odd.
For decoding, we use some of my Node.js scripts with the php str_repeat emulated method for iv:
var crypto = require('crypto'); function encrypt(data, key) { key = key || new Buffer(Core.config.crypto.cryptokey, 'binary'), cipher = crypto.createCipheriv('aes-256-cbc', key.toString('binary'), str_repeat('\0', 16)); cipher.update(data.toString(), 'utf8', 'base64'); return cipher.final('base64'); } function str_repeat(input, multiplier) { var y = ''; while (true) { if (multiplier & 1) { y += input; } multiplier >>= 1; if (multiplier) { input += input; } else { break; } } return y; }
NOTE. Static IV ( Initialization Vector ) is not recommended! NOTE. The JavaScript part is for Node.js using the crypto library.
I hope this works for you.
Maximilian walter
source share