Files
SEONexus/code/public/template/shikong/js/public-dec.js
2025-04-21 17:49:17 +08:00

103 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const EncAndDec = {
AesKey: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgnvJ0Xw7etrzacM4fIZIY6o',
/**
* AES加密
* @param {*} word 需要加解密的文本
* @param {*} key 加解密的秘钥
* iv: 偏移量最短8位数ECB模式不需要此参数
* @returns
*/
encryptData(data, key = this.AesKey) {
// 将数据转换为 WordArray
const dataWA = CryptoJS.enc.Utf8.parse(data);
// 将密钥转换为 WordArray
const keyWA = CryptoJS.enc.Utf8.parse(key);
// 生成随机 IV初始化向量
const iv = CryptoJS.lib.WordArray.random(16);
// 加密
const encrypted = CryptoJS.AES.encrypt(dataWA, keyWA, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
// 将 IV 和密文组合在一起
const combined = iv.concat(encrypted.ciphertext);
// 将组合后的数据转换为 Base64 字符串
return CryptoJS.enc.Base64.stringify(combined);
},
/**
* AES解密
* @param string word
* @param string key
* @returns string
*/
Decrypt(word = '', key = this.AesKey) {
if (word.length == 0) return null
let decodeBase64Str = atob(word)
let signData = decodeBase64Str.slice(16);
let encrypted = atob(signData);
encrypted = JSON.parse(encrypted);
const iv = CryptoJS.enc.Base64.parse(encrypted.iv);
const value = encrypted.value;
key = CryptoJS.enc.Base64.parse(key);
var decrypted = CryptoJS.AES.decrypt(value, key, {
iv: iv
});
decrypted = decrypted.toString(CryptoJS.enc.Utf8);
return decrypted;
},
decryptData(encryptedData, key = this.AesKey) {
// 将 Base64 编码的字符串转换为 WordArray
const encryptedDataWA = CryptoJS.enc.Base64.parse(encryptedData);
// 提取 IV初始化向量。CryptoJS WordArray 对象的 sigBytes 属性表示字节数
const iv = CryptoJS.lib.WordArray.create(encryptedDataWA.words.slice(0, 4), 16);
// 提取密文
const ciphertext = CryptoJS.lib.WordArray.create(encryptedDataWA.words.slice(4), encryptedDataWA.sigBytes - 16);
// 转换密钥为 WordArray
const keyWA = CryptoJS.enc.Utf8.parse(key);
// 解密
const decrypted = CryptoJS.AES.decrypt({ ciphertext: ciphertext }, keyWA, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
// 将解密结果转换为 UTF8 字符串
return decrypted.toString(CryptoJS.enc.Utf8);
},
/**
* 图片解密
* @param string strImgBase64
* @param object GetData
* @returns string
*/
DecryptImg(strImgBase64, GetData) {
let strBase64Content = `data:image/png;base64,${strImgBase64}`;
let strImgId = "#" + GetData.id + ""
let strImgUrl = $(strImgId).attr('data-original')
if (strImgUrl == GetData.imgurl) {
$(strImgId).attr('src', strBase64Content)
$(strImgId).removeClass("jqlazyload");
setImgHeightFun(strImgId)
}
return strBase64Content
},
}