decipher.final():不支持的状态或无法验证数据

发布于 2025-01-18 09:19:26 字数 1473 浏览 1 评论 0原文

我有以下代码:

const crypto = require("crypto");

const algorithm = "aes-256-gcm"; 
const message = "This is a secret message";
const iv = crypto.randomBytes(12);
const sKey = crypto.randomBytes(32);

const cipher = crypto.createCipheriv(algorithm, sKey, iv);
const decipher = crypto.createDecipheriv(algorithm, sKey, iv)

let encryptedData = cipher.update(message, "utf-8", "hex");
encryptedData += cipher.final("hex");

let decData = decipher.update(encryptedData, "hex", "utf-8");
decData += decipher.final("utf-8");

console.log("Encrypted message: " + encryptedData);
console.log("Decrypted message: ", decData)

当我运行它时,我收到以下异常:

node:internal/crypto/cipher:193
  const ret = this[kHandle].final();
                            ^

Error: Unsupported state or unable to authenticate data
    at Decipheriv.final (node:internal/crypto/cipher:193:29)
    at Object.<anonymous> ([...]/crypto-test.js:15:21)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

调用decipher.final()时抛出错误。我该如何修复它?

I have the following code:

const crypto = require("crypto");

const algorithm = "aes-256-gcm"; 
const message = "This is a secret message";
const iv = crypto.randomBytes(12);
const sKey = crypto.randomBytes(32);

const cipher = crypto.createCipheriv(algorithm, sKey, iv);
const decipher = crypto.createDecipheriv(algorithm, sKey, iv)

let encryptedData = cipher.update(message, "utf-8", "hex");
encryptedData += cipher.final("hex");

let decData = decipher.update(encryptedData, "hex", "utf-8");
decData += decipher.final("utf-8");

console.log("Encrypted message: " + encryptedData);
console.log("Decrypted message: ", decData)

When I run it, I receive the following exception:

node:internal/crypto/cipher:193
  const ret = this[kHandle].final();
                            ^

Error: Unsupported state or unable to authenticate data
    at Decipheriv.final (node:internal/crypto/cipher:193:29)
    at Object.<anonymous> ([...]/crypto-test.js:15:21)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

Error is thrown when calling decipher.final(). How can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娜些时光,永不杰束 2025-01-25 09:19:26

当使用经过身份验证的密码(即 aes-256-gcm)时,您将需要密码中的 auth 标签;否则 Decipher.final() 将抛出(如您所见),因为它具有(或在本例中没有)的身份验证标记与它认为解密文本的身份验证标记不匹配。

const crypto = require('crypto');

const algorithm = 'aes-256-gcm';
const message = 'This is a secret message';
const iv = crypto.randomBytes(12);
const sKey = crypto.randomBytes(32);

const cipher = crypto.createCipheriv(algorithm, sKey, iv);

let encryptedData = cipher.update(message, 'utf-8', 'hex');
encryptedData += cipher.final('hex');

const authTag = cipher.getAuthTag().toString("hex");  // <- new
console.log({authTag, encryptedData});  // for debugging

const decipher = crypto.createDecipheriv(algorithm, sKey, iv);
decipher.setAuthTag(Buffer.from(authTag, 'hex'));  // <- new
let decData = decipher.update(encryptedData, 'hex', 'utf-8');
decData += decipher.final('utf-8');
console.log('Decrypted message: ', decData)

When using an authenticated cipher (which aes-256-gcm is), you'll need the auth tag from the cipher; otherwise Decipher.final() will throw (as you can see) since the auth tag it has (or in this case, doesn't) doesn't match what it thinks the deciphered text's auth tag is.

const crypto = require('crypto');

const algorithm = 'aes-256-gcm';
const message = 'This is a secret message';
const iv = crypto.randomBytes(12);
const sKey = crypto.randomBytes(32);

const cipher = crypto.createCipheriv(algorithm, sKey, iv);

let encryptedData = cipher.update(message, 'utf-8', 'hex');
encryptedData += cipher.final('hex');

const authTag = cipher.getAuthTag().toString("hex");  // <- new
console.log({authTag, encryptedData});  // for debugging

const decipher = crypto.createDecipheriv(algorithm, sKey, iv);
decipher.setAuthTag(Buffer.from(authTag, 'hex'));  // <- new
let decData = decipher.update(encryptedData, 'hex', 'utf-8');
decData += decipher.final('utf-8');
console.log('Decrypted message: ', decData)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文