我需要转换此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');