forked from GabrielStetco/aes256cbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes256cbc.js
25 lines (21 loc) · 821 Bytes
/
aes256cbc.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
import crypto from "crypto";
const algorithm = "aes-256-cbc";
const key = "put-your-key-in-here";
const _iv = "put-your-valid-initialization-vector"; // LENGTH 16
const encrypt = (plaintext) => {
const sha256sum = crypto.createHash("sha256");
const _key = sha256sum.update(key).digest();
const cipher = crypto.createCipheriv(algorithm, _key, _iv);
const encrypted =
cipher.update(plaintext, "binary", "hex") + cipher.final("hex");
return encrypted;
};
const decrypt = (encrypted) => {
const sha256sum = crypto.createHash("sha256");
const _key = sha256sum.update(key).digest();
const decipher = crypto.createDecipheriv(algorithm, _key, _iv);
const decrypted =
decipher.update(encrypted, "hex", "binary") + decipher.final("binary");
return decrypted;
};
export default { encrypt, decrypt };