Created
October 27, 2018 13:34
-
-
Save authmane512/9c74b0270a4b10a5ccc202136a6b000b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For encryption:
https://gist.github.com/authmane512/3b14bbc2335bb8ffc30004b29d8b4de5