Skip to content

Instantly share code, notes, and snippets.

@maxpowel
Created October 25, 2022 12:07
Show Gist options
  • Save maxpowel/70b4340ea8417ebe2c4f76db482a18a2 to your computer and use it in GitHub Desktop.
Save maxpowel/70b4340ea8417ebe2c4f76db482a18a2 to your computer and use it in GitHub Desktop.
AES using typescript
import { cipher, util } from "node-forge";
# npm i --save-dev @types/node-forge
const secretKey = "12a97d88d634afdf9f4da5bd35223f01";
const iv = "5bf11a0951fa";
# Doing encryption
const cipherGCM = cipher.createCipher("AES-GCM", this.secretKey);
cipherGCM.start({iv: this.iv});
cipherGCM.update(util.createBuffer("Test message"))
cipherGCM.finish();
let out = cipherGCM.output.getBytes();
let encodedEncryptedData = util.encode64(out);
console.log(encodedEncryptedData)
# Decrypt the message
const decipher = cipher.createDecipher("AES-GCM", this.secretKey);
decipher.mode.tag = cipherGCM.mode.tag
decipher.start({iv: this.iv, tag: decipher.mode.tag});
out = util.decode64(encodedEncryptedData)
decipher.update(util.createBuffer(out))
var result = decipher.finish();
console.log(decipher.output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment