You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.2 KiB
36 lines
1.2 KiB
// const CryptoJS = require('crypto-js'); //引用AES源码js
|
|
// const CryptoJS = require('./crypto-js.js'); //引用AES源码js
|
|
const initKey = '5F6B2AK33DASD1235E74C231B47AC8F6';
|
|
const CryptoJS = require('./cryptoJs.js'); //引用AES源码js
|
|
/**
|
|
* AES加密
|
|
*/
|
|
export const AESEncrypt = (str, key) => {
|
|
let _key = key || initKey;
|
|
const __key = CryptoJS.enc.Utf8.parse(_key) //将秘钥转换成Utf8字节数组
|
|
// const iv = CryptoJS.enc.Utf8.parse(_key.substr(0, 16))
|
|
//加密
|
|
const encrypt = CryptoJS.AES.encrypt(JSON.stringify(str), __key, {
|
|
// iv: iv,
|
|
mode: CryptoJS.mode.ECB,
|
|
padding: CryptoJS.pad.Pkcs7
|
|
})
|
|
return encrypt.toString()
|
|
}
|
|
|
|
/**
|
|
* AES解密
|
|
*/
|
|
export const AESDecrypt = (str, key) => {
|
|
let _key = key || initKey;
|
|
const __key = CryptoJS.enc.Utf8.parse(_key) //将秘钥转换成Utf8字节数组
|
|
|
|
let base64data = CryptoJS.enc.Base64.parse(str);
|
|
let src = CryptoJS.enc.Base64.stringify(base64data);
|
|
const decrypt = CryptoJS.AES.decrypt(src, __key, {
|
|
mode: CryptoJS.mode.ECB,
|
|
padding: CryptoJS.pad.Pkcs7
|
|
})
|
|
const _decrypt = JSON.parse(decrypt.toString(CryptoJS.enc.Utf8)) //解密后的数据
|
|
return _decrypt
|
|
} |