在 PHP 中,当您将 mcrypt_module_open 与 Rijndael-128 一起使用时,您必须传递一个 32 字节密钥 和一个 16 字节 IV。
所以对于nodeJs,你可以使用crypto模块,例如:
var crypto = require('crypto');
//Declare our secret key
var key = 'fcda0ssdegfffc9441581bdd86484513dd9cb1547df2jsd';
//Declare our alogrithm
var algorithm = 'AES-256-CFB';
//Generate IV to hex format, so 8 * 2 = 16 bytes
var iv = crypto.randomBytes(8).toString('hex');
//Declare our string to encrypt
var password = 'HELLO WORLD';
var handler = {};
handler.encrypt64 = function(algorithm, key, vector, password){
//create a cipher with an IV
var cipher = crypto.createCipheriv(algorithm, key.substr(0,32), iv);
//Encrypt our password from utf8 to hex
var encrypted = cipher.update(password, 'utf8', 'base64');
//Return any remaining enciphered data to hex format
encrypted += cipher.final('base64');
//Return a base64 String
return encrypted;
};
handler.decrypt64 = function(algorithm, key, vector, password){
//create a decipher with an IV
var decipher = crypto.createDecipheriv(algorithm, key.substr(0, 32), iv);
//Decrypt our encode data from base64 to utf8
var decrypted = decipher.update(encode, 'base64', 'utf8');
decrypted += decipher.final('utf8');
//Return a utf8 string
return decrypted;
};
var encode = handler.encrypt64(algorithm, key, iv, password);
var decode = handler.decrypt64(algorithm, key, iv, encode);