如何将这个 mcrypt php 代码转换为 nodejs 中的相同代码



我需要转换这个php代码:

$cipher_alg = MCRYPT_TRIPLEDES;
$key = "thekey";
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg, MCRYPT_MODE_ECB), MCRYPT_RAND);
$encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv);
return base64_encode($encrypted_string);

到 nodejs。

我使用 https://github.com/tugrul/node-mcrypt 进行了测试,但使用相同的字符串,加密结果并不相同:

代码节点js测试:

let blowfishCfb = new MCrypt('tripledes', 'ecb');
let iv = blowfishCfb.generateIv();
blowfishCfb.validateKeySize(false);
blowfishCfb.validateIvSize(false);
blowfishCfb.open('thekey', iv);
let ciphertext = blowfishCfb.encrypt(text);
return Buffer.concat([iv, ciphertext]).toString('base64');

你能帮忙理解这一点吗?

谢谢

不要将

IV 和密码连接在一起:

let blowfishCfb = new MCrypt('tripledes', 'ecb');
let iv = blowfishCfb.generateIv();
blowfishCfb.validateKeySize(false);
blowfishCfb.validateIvSize(false);
blowfishCfb.open('thekey', iv);
let ciphertext = blowfishCfb.encrypt(text);
return ciphertext.toString('base64');

最新更新