bonus-ui/src/utils/aescbc.js

48 lines
1.0 KiB
JavaScript
Raw Normal View History

2024-07-04 10:35:24 +08:00
import * as CryptoJS from 'crypto-js'
const cbc_key = CryptoJS.enc.Utf8.parse('zhgd@bonus@zhgd@bonus@1234567890')
const cbc_iv = CryptoJS.enc.Utf8.parse('1234567812345678')
/**
* 加解密开关
* 默认参数需要加密
* @type {boolean}
*/
2024-07-21 08:53:09 +08:00
const encryptEnabled= true;
// /**
// * 默认后台会自动加密
// * @type {boolean}
// */
// const decryptEnabled=true;
2024-07-04 10:35:24 +08:00
/**
* 加密
* @param word
* @returns {string}
*/
export const encryptCBC = function(word) {
2024-07-08 09:55:01 +08:00
if(!encryptEnabled){
2024-07-04 10:35:24 +08:00
return word;
}
const srcs = CryptoJS.enc.Utf8.parse(word)
const encrypted = CryptoJS.AES.encrypt(srcs, cbc_key, {
iv: cbc_iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
return encrypted.toString()
}
/**
* 解密
* @param word
* @returns {*}
*/
export const decryptCBC = function(word) {
2024-07-21 08:53:09 +08:00
// if(!decryptEnabled){
// return word;
// }
2024-07-04 10:35:24 +08:00
const encrypted = CryptoJS.AES.decrypt(word, cbc_key, {
iv: cbc_iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
return encrypted.toString(CryptoJS.enc.Utf8)
}