Skip to content

Instantly share code, notes, and snippets.

@thEpisode
Created September 18, 2017 06:46
Show Gist options
  • Save thEpisode/8468f317c0f1f7a8ebaac8412da2aeb2 to your computer and use it in GitHub Desktop.
Save thEpisode/8468f317c0f1f7a8ebaac8412da2aeb2 to your computer and use it in GitHub Desktop.
Basic usage of AES implementation in Node.js
/// Dependencies
const _config = require('config'); // Current version ^1.20.4
const _crypto = require("crypto-js"); // Current version ^3.1.9-1
/// Properties
var _password = _config.aesPassword; // Good recomendation,
// don't save passwords
// in plain text or source code
var _textToEncrypt = 'Hello World!';
const encrypt = function (text) {
return (_crypto.AES.encrypt(text, _password)).toString();
}
const decrypt = function (ciphertext) {
var bytes = _crypto.AES.decrypt(ciphertext, _password);
return bytes.toString(_crypto.enc.Utf8);
}
var _cipherText = encrypt(_textToEncrypt);
var _uncipherText = decrypt(_cipherText);
console.log(`Encrypted message: ${_cipherText}`);
console.log(`Decrypted message: ${_uncipherText}`);
/*
* Using czfP2jvyUn7wwrZU8k7U as password:
* $ Encrypted message: U2FsdGVkX19jEOgGQ0GA9VHYVg53lp4JQXLkBKf7ZT4=
* $ Decrypted message: Hello World!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment