我正在使用nodejs来加密和解密aes-192-gcm
这是我的代码:
const encrypted = decrypt.encryptText('aes-192-gcm', 'FnpkKuIoqZL5B3tnE0Htmg==', '1z3FtB6OitmFOIsP', 'helloWorld', 'base64');
const de = decrypt.decryptText('aes-192-gcm', 'FnpkKuIoqZL5B3tnE0Htmg==', '1z3FtB6OitmFOIsP', encrypted, 'utf-8');
console.log(encrypted);
console.log(de);
使用的函数:
function encryptText(cipher_alg, key, iv, text, encoding) {
var cipher = crypto.createCipheriv(cipher_alg, key, iv);
encoding = encoding || "binary";
var result = cipher.update(text, "utf8", encoding);
result += cipher.final(encoding);
return result;
}
function decryptText(cipher_alg, key, iv, text, encoding) {
const decipher = crypto.createDecipheriv(cipher_alg, key, iv);
encoding = encoding || "binary";
let result = decipher.update(text, encoding);
result += decipher.final();
return result;
}
我收到的错误:
Unsupported state or unable to authenticate data
最佳答案
NodeJS 的加密模块使用 OpenSSL。此 API 具有 GCM/AEAD 密码的特殊参数。 API 中添加了使用它们的方法,例如 getAuthTag
和 setAuthTag
。如果没有后者,该方法总是抛出 GCM 模式解密异常。 (幸运的是)该标签不被视为 NodeJS/OpenSSL 中密文的一部分。其他语言运行时(例如 Java)确实将其视为密文的一部分。
关于aes-gcm - 无法解密 aes-192-gcm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57298919/