[英]mcrypt_encrypt in CFB mode function in CryptoJS
I'm trying to convert an mcrypt_encrypt
function written in php to node.js, I am using CryptoJS in node.js and tried all kinds of config options and I am not having any luck figuring this thing out. 我正在尝试将用php编写的
mcrypt_encrypt
函数转换为node.js,我在node.js中使用CryptoJS并尝试了各种配置选项,但我没有运气来弄清楚这件事。
PHP CODE: PHP代码:
base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,md5($secretKey),$ldapPwd,MCRYPT_MODE_CFB,$initialVector))
JavaScript Code That I tried do not know what I am doing wrong: 我尝试过的JavaScript代码不知道我做错了什么:
var encrypted = CryptoJS.AES.encrypt(password, keyBase64,
{
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CFB,
padding: CryptoJS.pad.NoPadding
});
The Cipher Feedback (CFB) mode is a family of block cipher modes with a segment size parameter. 密码反馈(CFB)模式是带有段大小参数的一组块密码模式。 Mcrypt only supports CFB8 whereas CryptoJS only supports CFB128.
Mcrypt仅支持CFB8,而CryptoJS仅支持CFB128。 They are incompatible.
它们不兼容。
I've implemented CFB b in CryptoJS which also supports 8-bit segments, but it is only tested for CryptoJS 3.1.2 and not the CryptoJS 2 version which is available through NPM. 我已经在CryptoJS中实现了CFB b ,它也支持8位段,但是仅针对CryptoJS 3.1.2进行了测试,而不是通过NPM提供的CryptoJS 2版本。
In PHP, when you use mcrypt_module_open
with Rijndael-128, you have to pass a 32 byte key and a 16 byte IV. 在PHP中,将
mcrypt_module_open
与Rijndael-128一起使用时,必须传递32字节的密钥和16字节的 IV。
So with nodeJs, you can use the crypto module, for example : 因此,使用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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.