Reputation:
I'm using nodejs to encrypt and decrypt aes-192-gcm
here's my code:
const encrypted = decrypt.encryptText('aes-192-gcm', 'FnpkKuIoqZL5B3tnE0Htmg==', '1z3FtB6OitmFOIsP', 'helloWorld', 'base64');
const de = decrypt.decryptText('aes-192-gcm', 'FnpkKuIoqZL5B3tnE0Htmg==', '1z3FtB6OitmFOIsP', encrypted, 'utf-8');
console.log(encrypted);
console.log(de);
Functions used:
function encryptText(cipher_alg, key, iv, text, encoding) {
var cipher = crypto.createCipheriv(cipher_alg, key, iv);
encoding = encoding || "binary";
var result = cipher.update(text, "utf8", encoding);
result += cipher.final(encoding);
return result;
}
function decryptText(cipher_alg, key, iv, text, encoding) {
const decipher = crypto.createDecipheriv(cipher_alg, key, iv);
encoding = encoding || "binary";
let result = decipher.update(text, encoding);
result += decipher.final();
return result;
}
The error I'm getting:
Unsupported state or unable to authenticate data
Upvotes: 2
Views: 789
Reputation: 12176
Couple of problems
I have attached a sample code based on the snippet you have shared.
var cipherTag;
const encrypted = encryptText('aes-192-gcm', 'FnpkKuIoqZL5B3tnE0Htmg==', '1z3FtB6OitmFOIsP', 'helloWorld', 'base64');
const de = decryptText('aes-192-gcm', 'FnpkKuIoqZL5B3tnE0Htmg==', '1z3FtB6OitmFOIsP', encrypted, 'base64');
console.log(encrypted);
console.log(de);
function encryptText(cipher_alg, key, iv, text, encoding) {
var cipher = crypto.createCipheriv(cipher_alg, key, iv);
encoding = encoding || "binary";
var result = cipher.update(text, "utf8", encoding);
result += cipher.final(encoding);
cipherTag = cipher.getAuthTag();
return result;
}
function decryptText(cipher_alg, key, iv, text, encoding) {
const decipher = crypto.createDecipheriv(cipher_alg, key, iv);
encoding = encoding || "binary";
decipher.setAuthTag(cipherTag);
let result = decipher.update(text, encoding, 'utf8');
result+= decipher.final('utf8');
return result.toString();
}
// Will output
b2SMQRBt/EgNgQ==
helloWorld
Upvotes: 2
Reputation: 94078
The crypto module of NodeJS uses OpenSSL. This API has special parameters for GCM / AEAD ciphers. Methods to use them have been added to the API, such as getAuthTag
and setAuthTag
. Without the latter, the method always throws an exception for GCM mode decryption. The tag is (fortunately) not considered part of the ciphertext in NodeJS / OpenSSL. Other languages runtimes - such as Java - do consider it part of the ciphertext.
Upvotes: 2