Node 中的 aes-128-gcm “状态不受支持或无法验证数据”

Van*_*ita 10 encryption node.js aes-gcm

我正在尝试使用节点加密提供的 aes-128-gcm 来实现加密/解密功能。根据我的理解,gcm 会加密密文,但也会对其进行哈希处理,并将其提供为“身份验证标签”。但是,我不断收到错误:“状态不受支持或无法验证数据”。

我不确定这是否是我的代码中的错误 - 查看加密的密文和身份验证标记,解密函数获取的密文与加密函数生成的密文和身份验证标记相同。

    function encrypt(plaintext) {
    // IV is being generated for each encryption
    var iv = crypto.randomBytes(12),
        cipher = crypto.createCipheriv(aes,key,iv),
        encryptedData = cipher.update(plaintext),
        tag;

    // Cipher.final has been called, so no more encryption/updates can take place
    encryptedData += cipher.final();

    // Auth tag must be generated after cipher.final()
    tag = cipher.getAuthTag();

    return encryptedData + "$$" + tag.toString('hex') + "$$" + iv.toString('hex');
}

function decrypt(ciphertext) {
    var cipherSplit = ciphertext.split("$$"),
        text = cipherSplit[0],
        tag = Buffer.from(cipherSplit[1], 'hex'),
        iv = Buffer.from(cipherSplit[2], 'hex'),
        decipher = crypto.createDecipheriv(aes,key,iv);

    decipher.setAuthTag(tag);

    var decryptedData = decipher.update(text);

    decryptedData += decipher.final();
}
Run Code Online (Sandbox Code Playgroud)

该错误是由 decipher.final() 引发的。

Evg*_*kov 17

如果有人仍然试图获得加密和解密过程的工作示例。

我留下了一些应该考虑的评论。

import * as crypto from 'crypto';

const textToEncode = 'some secret text'; // utf-16
const algo = 'aes-128-gcm';

// Key bytes length depends on algorithm being used:
// 'aes-128-gcm' = 16 bytes
// 'aes-192-gcm' = 24 bytes
// 'aes-256-gcm' = 32 bytes
const key = crypto.randomBytes(16);

const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algo, key, iv);

const encrypted = Buffer.concat([
  cipher.update(Buffer.from(textToEncode, 'utf-8')),
  cipher.final(),
]);

const authTag = cipher.getAuthTag();

console.info('Value encrypted', {
  valueToEncrypt: textToEncode,
  encryptedValue: encrypted.toString('hex'),
  authTag: authTag.toString('hex'),
});

// It's important to use the same authTag and IV that were used during encoding
const decipher = crypto.createDecipheriv(algo, key, iv);
decipher.setAuthTag(authTag);

const decrypted = Buffer.concat([
  decipher.update(encrypted),
  decipher.final(),
]);

console.info('Value decrypted', {
  valueToDecrypt: encrypted.toString('hex'),
  decryptedValue: decrypted.toString('utf-8'),
});
Run Code Online (Sandbox Code Playgroud)


Van*_*ita 9

我设法解决了这个问题:问题是我没有为 cipher.final() 指定编码类型,并且我在 String 中返回它,因此它没有返回 Buffer 对象,而 decipher.final() 是期待。

为了解决这个问题,我在 cipher.update 和 cipher.final 中将“utf-8”添加到“hex”编码参数中,反之亦然。

编辑添加代码示例 - 请注意这是 2018 年的,所以现在可能已经过时了。

function encrypt(plaintext) {
    // IV is being generated for each encryption
    var iv = crypto.randomBytes(12),
        cipher = crypto.createCipheriv(aes,key,iv),
         encryptedData = cipher.update(plaintext, 'utf-8', 'hex'),
        tag;

    // Cipher.final has been called, so no more encryption/updates can take place
     encryptedData += cipher.final('hex');

    // Auth tag must be generated after cipher.final()
    tag = cipher.getAuthTag();

    return encryptedData + "$$" + tag.toString('hex') + "$$" + iv.toString('hex');
}

function decrypt(ciphertext) {
    var cipherSplit = ciphertext.split("$$"),
        text = cipherSplit[0],
         tag = Buffer.from(cipherSplit[1], 'hex'),
            iv = Buffer.from(cipherSplit[2], 'hex'),
            decipher = crypto.createDecipheriv(aes, key, iv);


    decipher.setAuthTag(tag);

    var decryptedData = decipher.update(text, 'hex', 'utf-8');

   decryptedData += decipher.final('utf-8');
}
Run Code Online (Sandbox Code Playgroud)

  • 您能否分享一个执行此操作的代码示例:) (34认同)
  • 如果您使用 GCM 或类似工具,请确保设置 authTag /sf/ask/3728839271/ (2认同)