Skip to content

Instantly share code, notes, and snippets.

@joeneldeasis
Forked from anned20/encryption.js
Created June 24, 2021 06:09
Show Gist options
  • Save joeneldeasis/451d8cc62e6c88a161be5566a8651b36 to your computer and use it in GitHub Desktop.
Save joeneldeasis/451d8cc62e6c88a161be5566a8651b36 to your computer and use it in GitHub Desktop.
Encrypting files with NodeJS
const crypto = require('crypto');
const algorithm = 'aes-256-ctr';
let key = 'MySuperSecretKey';
key = crypto.createHash('sha256').update(String(key)).digest('base64').substr(0, 32);
const encrypt = (buffer) => {
// Create an initialization vector
const iv = crypto.randomBytes(16);
// Create a new cipher using the algorithm, key, and iv
const cipher = crypto.createCipheriv(algorithm, key, iv);
// Create the new (encrypted) buffer
const result = Buffer.concat([iv, cipher.update(buffer), cipher.final()]);
return result;
};
const decrypt = (encrypted) => {
// Get the iv: the first 16 bytes
const iv = encrypted.slice(0, 16);
// Get the rest
encrypted = encrypted.slice(16);
// Create a decipher
const decipher = crypto.createDecipheriv(algorithm, key, iv);
// Actually decrypt it
const result = Buffer.concat([decipher.update(encrypted), decipher.final()]);
return result;
};
const plain = Buffer.from('Hello world');
const encrypted = encrypt(plain);
console.log('Encrypted:', encrypted.toString());
const decrypted = decrypt(encrypted);
console.log('Decrypted:', decrypted.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment