Skip to content

Instantly share code, notes, and snippets.

@srsandy
Created November 16, 2019 17:24
Show Gist options
  • Save srsandy/80858aa93f49743ec4b5e8aafe0f8305 to your computer and use it in GitHub Desktop.
Save srsandy/80858aa93f49743ec4b5e8aafe0f8305 to your computer and use it in GitHub Desktop.
Encrypt and Decrypt emails
const crypto = require('crypto');
const algorithm = 'aes-192-cbc';
const password = '3zTvzr3p67VC61jmV54rIYu1545x4TlY';
const key = crypto.scryptSync(password, 'salt', 24);
const iv = Buffer.alloc(16, 0);
function encrypt(text) {
const cipher = crypto.createCipheriv(algorithm, key, iv)
let encrypted = cipher.update(text, 'utf8', 'hex')
encrypted += cipher.final('hex');
return encrypted;
}
function decrypt(encrypted) {
const decipher = crypto.createDecipheriv(algorithm, key, iv)
let dec = decipher.update(encrypted, 'hex', 'utf8')
dec += decipher.final('utf8');
return dec;
}
const email = "sandeepranjan2007@yahoo.co.in";
const hw = encrypt(email);
console.log(decrypt(hw));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment