· 6 years ago · Mar 24, 2019, 07:44 AM
1const crypto = require('crypto');
2
3key = "secretKey";
4string = "your string";
5
6let encryptedString = encrypt(key,string);
7let decryptedString = decrypt(key,encryptedString);
8
9console.log(encryptedString);
10console.log(decryptedString);
11
12function encrypt(KEY,text){
13 const cipher = crypto.createCipher('aes256', KEY);
14 var encrypted = cipher.update(text,'utf8', 'hex');
15 encrypted += cipher.final('hex');
16 return encrypted;
17}
18
19function decrypt(KEY,encryptedText){
20 const decipher = crypto.createDecipher('aes256', KEY)
21 var decrypted = decipher.update(encryptedText,'hex','utf8')
22 decrypted += decipher.final('utf8');
23 return decrypted;
24}