Created
December 28, 2020 07:53
-
-
Save wpsteak/300a766d682e4e0cabf859cb0a1f04e6 to your computer and use it in GitHub Desktop.
index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const buffer = require('buffer'); | |
const crypto = require('crypto'); | |
const aes256gcm = (key, iv) => { | |
const ALGO = 'aes-256-gcm'; | |
const encrypt = (str) => { | |
const cipher = crypto.createCipheriv(ALGO, key, iv); | |
let enc = cipher.update(str, 'utf8', 'hex'); | |
enc += cipher.final('hex'); | |
return [enc, iv, cipher.getAuthTag()]; | |
}; | |
const decrypt = (enc, iv, authTag) => { | |
const decipher = crypto.createDecipheriv(ALGO, key, iv); | |
decipher.setAuthTag(authTag); | |
let str = decipher.update(enc, 'hex', 'utf8'); | |
str += decipher.final('utf8'); | |
return str; | |
}; | |
return { | |
encrypt, | |
decrypt, | |
}; | |
}; | |
const KEY = Buffer.from('3a7bd3e2360a3d29eea436fcfb7e44c7', 'utf8'); | |
const IV = Buffer.from('35d117c42d1c1835420b6b9942dd4f1b', 'utf8'); | |
const aesCipher = aes256gcm(KEY, IV); | |
const [encrypted, iv, authTag] = aesCipher.encrypt('BPJ1AXL5TXAoBcEhHXEvHx=='); | |
console.log("encrypted:"); | |
console.log(encrypted) | |
console.log("authTag:"); | |
console.log(authTag) | |
const decrypted = aesCipher.decrypt(encrypted, iv, authTag); | |
console.log("decrypted:"); | |
console.log(decrypted); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment