-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryption.js
37 lines (32 loc) · 1.22 KB
/
Encryption.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
27
28
29
30
31
32
33
34
35
36
37
const crypto = require('crypto');
// Key derivation
var password = 'hjrrffutdlyWkQdL7Oo';
var salt = crypto.randomBytes(16); // some random salt
var digest = 'sha256';
var length = 16;
var iterations = 10000;
var key = crypto.pbkdf2Sync(password, salt, iterations, length, digest);
//Checking the crypto module
const algorithm = 'aes-128-cbc'; //Using AES encryption
// const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);
//Encrypting text
function encrypt(text) {
let cipher = crypto.createCipheriv('aes-128-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') };
}
// Decrypting text
function decrypt(text) {
let iv = Buffer.from(text.iv, 'hex');
let encryptedText = Buffer.from(text.encryptedData, 'hex');
let decipher = crypto.createDecipheriv('aes-128-cbc', Buffer.from(key), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
// Text send to encrypt function
var hw = encrypt("ArZEtS0UtI6O7F1AmfV5sdffeeeeddg")
console.log(hw)
console.log(decrypt(hw))