Skip to content

Instantly share code, notes, and snippets.

@assafmo
Created May 26, 2020 13:42
Show Gist options
  • Save assafmo/38250c35b5fa475d4d2dc85bf121d136 to your computer and use it in GitHub Desktop.
Save assafmo/38250c35b5fa475d4d2dc85bf121d136 to your computer and use it in GitHub Desktop.
AES-256-GCM in javascript
const forge = require("node-forge");
let key = forge.util.createBuffer(new Uint8Array(new Array(32).fill(0x7)).buffer);
let iv = forge.util.createBuffer(new Uint8Array(new Array(12).fill(0x0)).buffer);
const input = forge.util.createBuffer();
input.putString("banana");
var cipher = forge.cipher.createCipher("AES-GCM", key);
cipher.start({ iv: iv });
cipher.update(input);
cipher.finish();
var encrypted = cipher.output;
// outputs encrypted hex
console.log(encrypted.toHex());
key = forge.util.createBuffer(new Uint8Array(new Array(32).fill(0x7)).buffer);
iv = forge.util.createBuffer(new Uint8Array(new Array(12).fill(0x0)).buffer);
var decipher = forge.cipher.createDecipher("AES-GCM", key);
decipher.start({ iv: iv, tag: cipher.mode.tag });
decipher.update(cipher.output);
var result = decipher.finish();
console.log(decipher.output.data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment