Skip to content

Instantly share code, notes, and snippets.

@jsuryahyd
Created April 19, 2020 10:34
Show Gist options
  • Save jsuryahyd/a748dd84376f66921882ea4160165cf9 to your computer and use it in GitHub Desktop.
Save jsuryahyd/a748dd84376f66921882ea4160165cf9 to your computer and use it in GitHub Desktop.
Encrypt and Decrypt in nodejs using crypto module
// https://gist.github.com/vlucas/2bd40f62d20c1d49237a109d491974eb
"use strict";
const { errorLog } = require("./logger");
const crypto = require("crypto");
const ENCRYPTION_KEY =
process.env.ENCRYPTION_KEY || "Dw-3]v,#FX@1gerbSAC4Jhn2=$!q/K.Z"; // Must be 256 bits (32 characters)
const IV_LENGTH = 16; // For AES, this is always 16
function encrypt(text, key = ENCRYPTION_KEY) {
if (typeof text == "undefined" || text == null) {
errorLog.error(new Error("no input given for encrypt function"));
return false;
}
if (typeof text != "string") {
text = text + "";
}
if (text == "") {
return "";
}
try {
let iv = crypto.randomBytes(IV_LENGTH);
let cipher = crypto.createCipheriv("aes-256-cbc", new Buffer(key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString("hex") + ":" + encrypted.toString("hex");
} catch (err) {
errorLog.debug ? errorLog.error(err) : errorLog.error(new Error(err));
return false;
}
}
function decrypt(text, key = ENCRYPTION_KEY) {
if (typeof text == "undefined" || text == null) {
errorLog.error(new Error("no input given for decrypt function"));
return false;
}
if (typeof text != "string") {
text = text + "";
}
if (text == "") {
return "";
}
try {
let textParts = text.split(":");
let iv = new Buffer(textParts.shift(), "hex");
let encryptedText = new Buffer(textParts.join(":"), "hex");
let decipher = crypto.createDecipheriv(
"aes-256-cbc",
new Buffer(ENCRYPTION_KEY),
iv
);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
} catch (err) {
errorLog.debug ? errorLog.error(err) : errorLog.error(new Error(err));
return false;
}
}
/**
* @link https://gist.github.com/csanz/1181250/351ea1a7363c8a80944d572c70b769931ee94994
*/
function simpleEncrypt(text, key = process.env.simpleEncryptKey) {
if (typeof text == "undefined" || text == null) {
errorLog.error(new Error("no input given for simple encrypt function"));
return false;
}
if (typeof text != "string") {
text = text + "";
}
if (text == "") {
return "";
}
try {
var cipher = crypto.createCipher("aes-256-cbc", key);
var crypted = cipher.update(text, "utf8", "hex");
crypted += cipher.final("hex");
return crypted;
} catch (err) {
errorLog.debug ? errorLog.error(err) : errorLog.error(new Error(err));
errorLog.error(err + " " + text);
return false;
}
}
function simpleDecrypt(text, key = process.env.simpleEncryptKey) {
if (typeof text == "undefined" || text == null) {
errorLog.error(new Error("no input given for simple decrypt function"));
return false;
}
if (typeof text != "string") {
text = text + "";
}
if (text == "") {
return "";
}
try {
var decipher = crypto.createDecipher("aes-256-cbc", key);
var dec = decipher.update(text, "hex", "utf8");
dec += decipher.final("utf8");
return dec;
} catch (err) {
errorLog.debug ? errorLog.error(err) : errorLog.error(new Error(err));
errorLog.error(err + " " + text);
return false;
}
}
module.exports = { decrypt, encrypt, simpleEncrypt, simpleDecrypt };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment