I want to encrypt some data in Node.js using an authenticated encryption scheme like AES-GCM.
If I run the following sample code
app.get("/test", function(req,res) {
var key = "12345678901234567890123456789012";
var iv = "123456789012";
var cipher = crypto.createCipheriv("id-aes256-GCM",key.toString("binary"),iv.toString("binary"));
var decipher = crypto.createDecipheriv("id-aes256-GCM",key.toString("binary"),iv.toString("binary"));
console.log(decipher.update(cipher.update("bla")));
console.log(decipher.update(cipher.final()));
console.log(decipher.final());
});
I don't get a console output but the error message "TypeError: DecipherFinal fail". If I use cipher AES-256-CTR instead of "id-aes256-GCM", this code works fine and prints "bla" on the console.
What am I doing wrong?
edit:
Further investigating shows, that cipher.update("bla") returns "â" (single character...strange) and cipher.final() returns an empty string. I think this can't be a correct ciphertext which should at least have the size of the plaintext...
cipher.update("bla")andcipher.final()? - Maarten Bodewesecho -n "bla" | openssl enc -e -id-aes256-GCM -nosalt -a -out t.outfollowed byopenssl enc -d -id-aes256-GCM -nosalt -a -in t.outyields the errorbad decrypt. Maybe a bug in OpenSSL? - fuzic