Skip to content

Instantly share code, notes, and snippets.

@dr5hn
Created November 11, 2020 07:47
Show Gist options
  • Save dr5hn/151cbdb9cc5515c9a5c923db6fcf86ab to your computer and use it in GitHub Desktop.
Save dr5hn/151cbdb9cc5515c9a5c923db6fcf86ab to your computer and use it in GitHub Desktop.
Encrypt and Decrypt file and text using cypto-js
const fs = require('fs');
const crypto = require('crypto');
// Generated pair of public & private keys
const cryptoPubKey = fs.readFileSync('public.pem');
const cryptoPriKey = fs.readFileSync('private.pem');
const algorithm = 'aes-256-cbc';
module.exports = {
async fileEncryption (file) {
return new Promise(function (resolve) {
var key = crypto.randomBytes(32);
// console.log('Unencrypted Key:', key.toString('base64'));
var iv = crypto.randomBytes(16);
var cipher = crypto.createCipheriv(algorithm, key, iv);
const encryptedKey = crypto.publicEncrypt(cryptoPubKey, key);
// console.log('Encrypted Key:', encryptedKey.toString('base64'));
var input = fs.createReadStream(file.path);
var output = fs.createWriteStream(file.path + '.enc');
input.pipe(cipher).pipe(output);
output.on('finish', function() {
// console.log('Encrypted file written to disk!');
resolve({
file: output,
key: encryptedKey,
iv: iv
});
});
});
},
async fileDecryption (data) {
return new Promise(function (resolve) {
// console.log('Encrypted Key Recieved:', data.key.toString('base64'));
const decryptedKey = crypto.privateDecrypt({
key: cryptoPriKey,
passphrase: process.env.ENCRYPTION_PASSPHRASE, // env variable
}, data.key);
// console.log('Decrypted Key:', decryptedKey.toString('base64'));
const decipher = crypto.createDecipheriv(algorithm, decryptedKey, data.iv);
var input = fs.createReadStream(data.file.path);
const filename = data.file.path.replace('.enc', '');
var output = fs.createWriteStream(filename);
input.pipe(decipher).pipe(output);
output.on('finish', function() {
// console.log('Decrypted file written to disk!');
resolve({
path: filename
});
});
});
},
async textEncryption(text) {
return new Promise(function (resolve) {
const textBuffer = Buffer.from(text, 'utf8');
const encryptedText = crypto.publicEncrypt({
key: cryptoPubKey,
}, textBuffer);
resolve(encryptedText);
});
},
async textDecryption(encryptedTextBuff) {
return new Promise(function (resolve) {
const decryptedText = crypto.privateDecrypt({
key: cryptoPriKey,
passphrase: process.env.ENCRYPTION_PASSPHRASE,
}, encryptedTextBuff);
resolve(decryptedText.toString('utf8'));
});
}
}
require('dotenv').config();
const crypto = require('crypto');
const fs = require('fs');
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: process.env.ENCRYPTION_PASSPHRASE // env variable
}
});
// Generate Public Key
if (!fs.existsSync('public.pem')) {
fs.appendFile('public.pem', publicKey, function (err) {
if (err) return console.log(err);
console.log('Generated Public Key');
});
} else {
console.log('Public Key has been already generated');
}
// Generate Private Key
if (!fs.existsSync('private.pem')) {
fs.appendFile('private.pem', privateKey, function (err) {
if (err) return console.log(err);
console.log('Generated Private Key');
});
} else {
console.log('Private Key has been already generated');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment