bonus-checkVerify-app/utils/aes.js

86 lines
2.7 KiB
JavaScript

import CryptoJS from 'crypto-js';
import base64 from 'base-64';
// import { TextEncoder } from 'text-decoding'
// const TextEncoder = new TextEncoder();
var aqEnnable = true;//是否开启安全验证
// const key = 'zhst@bonus@zhst@bonus@1234567890';//AES加密cbc 256
const key = 'pigxpigxpigxpigx';//AES加密cbc 256
// const key = '1234567812345678';//AES加密cbc
// const iv = '1234567812345678';
const iv = 'pigxpigxpigxpigx';
function getKey() {
// 真正的key
return CryptoJS.enc.Utf8.parse(key);
}
function getIv() {
// 真正的iv
return CryptoJS.enc.Utf8.parse(iv);
}
export default {
encrypt(word) {
if(!aqEnnable){
return word;
}
if(word==null){
var ciphertext = CryptoJS.AES.encrypt(word, getKey(), {
iv: getIv(),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
});
return ciphertext.toString();
}else{
var ciphertext = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(word), getKey(), {
iv: getIv(),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
});
return ciphertext.toString();
}
},
decrypt(word) {
if(!aqEnnable){
return word;
}
if(word==null){
return "";
}
// console.log(word)
// console.log(base64.decode(word))
var bytes = CryptoJS.AES.decrypt(word, getKey(), {
iv: getIv(),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
});
return bytes.toString(CryptoJS.enc.Utf8);
},
//
DateFormat(date, fmt) {
if (date && fmt) {
let _date = new Date(date);
var o = {
'Y+': _date.getFullYear() , //年
'M+': _date.getMonth() + 1, //月份
'd+': _date.getDate(), //日
'h+': _date.getHours(), //小时
'm+': _date.getMinutes(), //分
's+': _date.getSeconds(), //秒
'q+': Math.floor((_date.getMonth() + 3) / 3), //季度
S: _date.getMilliseconds(), //毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (_date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? (o)[k] : ('00' + (o)[k]).substr(('' + (o)[k]).length));
}
}
return fmt;
} else {
return '';
}
}
}