Skip to content

Instantly share code, notes, and snippets.

@bbstilson
Last active August 10, 2022 22:56
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bbstilson/2c4241f8e89e6af4539cd2f347a28a17 to your computer and use it in GitHub Desktop.
Save bbstilson/2c4241f8e89e6af4539cd2f347a28a17 to your computer and use it in GitHub Desktop.
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const AppendInitVect = require('./appendInitVect');
const getCipherKey = require('./getCipherKey');
function encrypt({ file, password }) {
// Generate a secure, pseudo random initialization vector.
const initVect = crypto.randomBytes(16);
// Generate a cipher key from the password.
const CIPHER_KEY = getCipherKey(password);
const readStream = fs.createReadStream(file);
const gzip = zlib.createGzip();
const cipher = crypto.createCipheriv('aes256', CIPHER_KEY, initVect);
const appendInitVect = new AppendInitVect(initVect);
// Create a write stream with a different file extension.
const writeStream = fs.createWriteStream(path.join(file + ".enc"));
readStream
.pipe(gzip)
.pipe(cipher)
.pipe(appendInitVect)
.pipe(writeStream);
}
@alimustafa1993
Copy link

How can I encrypt and decrypt integer number via streaming encryption by using constant key?

@bbstilson
Copy link
Author

This should be what you're looking for: https://stackoverflow.com/a/25650163

Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment