Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unsupported state or unable to authenticate data" with aes-128-gcm in Node

I'm trying to implement encrypt/decrypt functions using aes-128-gcm as provided by node crypto. From my understanding, gcm encrypts the ciphertext but also hashes it and provides this as an 'authentication tag'. However, I keep getting the error: "Unsupported state or unable to authenticate data".

I'm not sure if this is an error in my code - looking at the encrypted ciphertext and auth tag, the one being fetched by the decrypt function is the same as the one produced by the encrypt function.

    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();
}

The error is being thrown by decipher.final().

like image 511
Vanita Avatar asked Dec 21 '16 15:12

Vanita


2 Answers

In case if someone still tries to get a working example of encryption and decryption process.

I've left some comments that should be taken into consideration.

import * as crypto from 'crypto';

const textToEncode = 'some secret text'; // utf-8
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'),
});
like image 93
Evgeny Melnikov Avatar answered Nov 20 '22 04:11

Evgeny Melnikov


I managed to fix this: the issue was that I wasn't specifying an encoding type for cipher.final() and I was returning it within a String, so it wasn't returning a Buffer object, which decipher.final() was expecting.

To fix, I add 'utf-8' to 'hex' encoding parameters within my cipher.update and cipher.final, and vice versa in decipher.

like image 21
Vanita Avatar answered Nov 20 '22 04:11

Vanita