crypto 32 characers encryption node js

Solutions on MaxInterview for crypto 32 characers encryption node js by the best coders in the world

showing results for - "crypto 32 characers encryption node js"
Daniel
25 Feb 2016
1const crypto = require('crypto');
2const algorithm = 'aes-256-cbc';
3const key = 'pass'; //Here I would get the password of the user
4
5function encrypt(text) {
6   const iv = crypto.randomBytes(16);
7   let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
8   let encrypted = cipher.update(text);
9   encrypted = Buffer.concat([encrypted, cipher.final()]);
10   return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') };
11}
12
13function decrypt(text) {
14   let iv = Buffer.from(text.iv, 'hex');
15   let encryptedText = Buffer.from(text.encryptedData, 'hex');
16   let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), iv);
17   let decrypted = decipher.update(encryptedText);
18   decrypted = Buffer.concat([decrypted, decipher.final()]);
19   return decrypted.toString();
20}