Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const nacl = require('tweetnacl');
const util = require('tweetnacl-util');
const scrypt = require('scryptsy');
const fs = require('fs');
let password = "Node.js is cool";
// these are our previous salt and nonce from encryption.js:
let salt = new Uint8Array([ 232, 66, 12, 24, 90, 175, 58, 106, 13, 220, 241, 77, 156, 230, 140, 218 ]);
let nonce = new Uint8Array([ 143, 28, 216, 227, 2, 177, 160, 159, 243, 180, 138, 230, 142, 165, 28, 189, 208, 63, 130, 131, 204, 240, 105, 142 ]);
// Generate the key:
let N = 16384; // number of iterations
let r = 8; // Memory factor
let p = 1; // Parallelization factor
let key = scrypt(password, salt, N, r, p, nacl.secretbox.keyLength);
// Get encrypted data from file:
let content = fs.readFileSync('file.txt', 'ascii'); // no need of utf8 with Base64
console.log(content);
let encrypted = util.decodeBase64(content);
// Decrypt the data:
let decrypted = nacl.secretbox.open(encrypted, nonce, key);
console.log(util.encodeUTF8(decrypted)); // function should be named "decodeUTF8", they reversed names!
@authmane512

This comment has been minimized.

Copy link
Owner Author

authmane512 commented Oct 27, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.