尝试在 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;
}
}