-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_decrypt_encrypt.js
More file actions
66 lines (60 loc) · 1.92 KB
/
test_decrypt_encrypt.js
File metadata and controls
66 lines (60 loc) · 1.92 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const crypto = require("crypto");
const password = "nZr4u7w!z%C*F-Ja";
const algorithm = "aes-256-cbc";
let key = crypto
.createHash("sha256")
.update(String(password))
.digest("base64")
.substr(0, 32);
let iv = crypto
.createHash("sha256")
.update(String(password))
.digest("base64")
.substr(0, 16);
const encrypt_data = (data) => {
try {
const cipher = crypto.createCipheriv(algorithm, key, iv);
let crypted = cipher.update(data, "utf8", "hex");
crypted += cipher.final("hex");
return iv.toString("hex") + ":" + crypted; // Include IV for decryption
} catch (err) {
return err;
}
};
const encrypt_data_with_iv = async (data) => {
try {
let cipher = crypto.createCipheriv("aes-256-cbc", Buffer.from(key), iv);
let encrypted = cipher.update(data);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return encrypted.toString("hex");
} catch (err) {
return err;
}
};
const decrypt_data = async (data) => {
try {
let encryptedData = Buffer.from(data, "hex");
let decipher = crypto.createDecipheriv("aes-256-cbc", Buffer.from(key), iv);
let decrypted = decipher.update(encryptedData);
decrypted += decipher.final();
return decrypted.toString();
} catch (err) {
return err;
}
};
let email = "leitgabm@gmail.com";
// "johnfiewor@gmail.com";
// "wendy@connect2potential.com";
encrypt_data_with_iv(email).then((d) => console.log("encrypted email: ", d));
const encryptedEmail =
"ea9f7996521b5db6926b103abdb30240ec4ed9225252589bc7ff7f9d241df08a";
// "131ef539219f930ee576ae342425b81eff93e8e1aa50a2eace28895ff06f3b3d";
//"4cd950575989eed60275f74c34e58b76:e039ac84492c696b58ffc04f1118d3da"
// const run = async () => {
// const decryptedEmail = await (0, decrypt_data)(encryptedEmail);
// console.log("decryptedEmail: ", decryptedEmail);
// };
// run();
// 4cd950575989eed60275f74c34e58b76:e039ac84492c696b58ffc04f1118d3da
// "robbie@safe.ai";
// "anders@safe.ai";