我需要转换这个 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进行了测试但对于相同的字符串,加密结果并不相同:
测试的nodejs代码:
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');
关于php - 如何将这个mcrypt php代码转换为nodejs中的相同代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46806305/