我正在尝试将用php编写的mcrypt_encrypt
函数转换为node.js,我在node.js中使用CryptoJS并尝试了各种配置选项,我没有任何运气来解决这个问题
PHP代码:
base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,md5($secretKey),$ldapPwd,MCRYPT_MODE_CFB,$initialVector))
我试过的JavaScript代码不知道我做错了什么:
var encrypted = CryptoJS.AES.encrypt(password, keyBase64,
{
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CFB,
padding: CryptoJS.pad.NoPadding
});
答案 0 :(得分:1)
密码反馈(CFB)模式是一系列具有段大小参数的分组密码模式。 Mcrypt仅支持CFB8,而CryptoJS仅支持CFB128。它们是不相容的。
我已经实现了CFBb in CryptoJS,它也支持8位段,但它仅针对CryptoJS 3.1.2进行了测试,而不是通过NPM提供的CryptoJS 2版本。
答案 1 :(得分:1)
在PHP中,当您使用mcrypt_module_open
和Rijndael-128时,您必须传递 32字节密钥和 16字节 IV。
使用nodeJs,您可以使用加密模块,例如:
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);