尝试在 NodeJS 中解密时出现此错误。 [错误:不支持的状态或无法验证数据]

问题描述 投票:0回答:0

尝试在 NodeJS 中解密图像数据时出现上述错误。以下是详细信息。

  • 算法-aes-256-gcm
  • 加密-Golang
  • 解密-Nodejs
  • 数据加密 - 图片

加密Golang代码

 func EncryptData(data []byte, passphrase string) ([]byte, error) {
    block, _ := aes.NewCipher([]byte(createHash(passphrase)))
    gcm, err := cipher.NewGCM(block)
    if err != nil {
        return nil, err
    }
    nonce := make([]byte, gcm.NonceSize())
    if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
        return nil, err
    }
    ciphertext := gcm.Seal(nonce, nonce, data, nil)
}

解密NodeJS代码

 async function decrypt(data, enc_key) {

    if (!enc_key) {
        console.log("Encryption key is undefined")
        return
    }

    try {

        let decryptedEncKey = await encryptedKey(enc_key);
        console.log('The decrypted key inside decrypt function is: ', decryptedEncKey);

        const length = data.length;
        const iv = data.slice(0, IV_SIZE);
        const tag = data.slice(length - TAG_SIZE, length);
        let encryptedData = data.slice(IV_SIZE, length - TAG_SIZE);

        // AES 256 GCM Mode
        const decipher = crypto.createDecipheriv('aes-256-gcm', decryptedEncKey, iv);
        decipher.update(encryptedData, 'null', 'binary') + decipher.final('binary'));
        return decipher.update(encryptedData, 'null', 'binary') + decipher.final('binary');
    
    } catch (error) {
        console.error(error);
        console.log("Error in image decription");
        return null;
    }
}
node.js algorithm go encryption aes
© www.soinside.com 2019 - 2024. All rights reserved.