forked from vincentzyc/form-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.js
More file actions
33 lines (30 loc) · 957 Bytes
/
crypto.js
File metadata and controls
33 lines (30 loc) · 957 Bytes
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
const CryptoJS = require("crypto-js");
const AES = require("crypto-js/aes");
const utf8 = require("crypto-js/enc-utf8");
const md5 = require("crypto-js/md5");
const iv = utf8.parse("qwertyuiop123456");
const mode = CryptoJS.mode.CBC;
const padding = require("crypto-js/pad-pkcs7");
const default_key = "yuikey0123456789";
export default {
encrypt(data, key) {
if (typeof data !== "string") data = JSON.stringify(data);
let real_key = utf8.parse(md5(key || default_key).toString().slice(0, 16));
let bytes = AES.encrypt(data, real_key, {
iv: iv,
mode: mode,
padding: padding
});
return bytes.toString();
},
decrypt(data, key) {
if (!data || typeof data !== "string") return '';
let real_key = utf8.parse(md5(key || default_key).toString().slice(0, 16));
let bytes = AES.decrypt(data, real_key, {
iv: iv,
mode: mode,
padding: padding
});
return bytes.toString(utf8);
}
};