repl.it
@pyelias/

Crypto Test

Nodejs

No description

fork
loading
Files
  • index.js
  • db.json
  • decrypt.txt
  • encrypt.txt
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const crypto = require('crypto');
const key = '9WN9Qaxy22LjxVs2';

function encrypt(text) {
	var cipher = crypto.createCipher('aes-256-cbc', key);
	var crypted = cipher.update(text, 'utf8', 'hex');
	crypted += cipher.final('hex');
	return crypted;
}

function decrypt(text) {
	var decipher = crypto.createDecipher('aes-256-cbc', key);
	var dec = decipher.update(text, 'hex', 'utf8');
	dec += decipher.final('utf8');
	return dec;
}

var fileEncrypt = fs.readFileSync('encrypt.txt', 'utf8');
if (fileEncrypt && fileEncrypt !== '') {
	fs.writeFileSync('encrypt.txt', encrypt(fileEncrypt), 'utf8');
}

var fileDecrypt = fs.readFileSync('decrypt.txt', 'utf8');
if (fileDecrypt && fileDecrypt !== '') {
	fs.writeFileSync('decrypt.txt', decrypt(fileDecrypt), 'utf8');
}
node v10.16.0