我有一些php解密代码,我想在节点js中重写此解密。
$password = "password";
$salt = "salt";
$data = "somebase64data"
$prepared_key = openssl_pbkdf2($password, $salt, 256, 65536, "sha256");
$rtc = openssl_decrypt(base64_decode(data),"AES-256-CBC",
$prepared_key, OPENSSL_RAW_DATA, "LQjFLCU3sAVplBC3");
$rtc = gzdecode($rtc);
file_put_contents(dirname(__FILE__) . '/log/tcs.new.crypt.' . $o . '.rtc.log', $rtc);
我尝试这样做:
function decrypt(text, salt, iv) {
const password = "password";
crypto.pbkdf2(password, salt, 65536, 256, "sha256", (err, key) => {
try {
const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv);
const decrypted = decipher.update(text, "base64", "binary");
decrypted += decipher.final("binary");
} catch (error) {
throw new Error(error)
}
});
}
decrypt(plainText, salt, iv);
然后我出现此错误:密钥长度无效。 我知道pbkdf2之后的密钥是一个长度为256个字节的缓冲区。但是createDecipheriv函数需要一个长度为32个字节的密钥