2 changed files with 48 additions and 0 deletions
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
import CryptoJS from 'crypto-js' |
||||
import store from '@/store' |
||||
|
||||
export default class EncryptUtil { |
||||
/** |
||||
* 加密 |
||||
* @param {string} data |
||||
*/ |
||||
static encrypt(data) { |
||||
let key = CryptoJS.enc.Utf8.parse(this.getKey()) |
||||
let iv = CryptoJS.enc.Utf8.parse(this.getIv()) |
||||
let source = CryptoJS.enc.Utf8.parse(data) |
||||
|
||||
let encrypted = CryptoJS.AES.encrypt(source, key, { |
||||
iv, |
||||
mode: CryptoJS.mode.CBC, |
||||
padding: CryptoJS.pad.ZeroPadding |
||||
}) |
||||
console.log('encrypt ==> key = ' + this.getKey() + ', iv = ' + this.getIv() + ', source = ' + data + ', result = ' + encrypted.toString()) |
||||
return encrypted.toString() |
||||
} |
||||
|
||||
/** |
||||
* 解密 |
||||
* @param {string} data |
||||
*/ |
||||
static decrype(data) { |
||||
let key = CryptoJS.enc.Utf8.parse(this.getKey()) |
||||
let iv = CryptoJS.enc.Utf8.parse(this.getIv()) |
||||
|
||||
var decrypt = CryptoJS.AES.decrypt(data, key, { |
||||
iv, |
||||
mode: CryptoJS.mode.CBC, |
||||
padding: CryptoJS.pad.ZeroPadding |
||||
}) |
||||
console.log('decrypt ==> key = ' + this.getKey() + ', iv = ' + this.getIv() + ', source = ' + data + ', result = ' + decrypt.toString(CryptoJS.enc.Utf8)) |
||||
return decrypt.toString(CryptoJS.enc.Utf8) |
||||
} |
||||
|
||||
static getKey() { |
||||
return store.getters.encryptKey |
||||
} |
||||
|
||||
static getIv() { |
||||
return store.getters.encryptIv |
||||
} |
||||
} |
Loading…
Reference in new issue