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

89 lines
2.9 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: 'QvanVezUfeJep0euYWZph4Dvq6zv5vKxNAwnfB6fyiY=',
/**
* AES加密
* @param {*} word 需要加解密的文本
* @param {*} key 加解密的秘钥
* iv: 偏移量最短8位数ECB模式不需要此参数
* @returns
*/
// Encrypt(word, key = AesKey) {
// let srcs = JSON.stringify(word);
// let iv = crypto.randomBytes(16); // 16位的随机数
// let cipher = crypto.createCipheriv(algorithm, Buffer.from(key), iv);
// let encrypted = cipher.update(srcs);
// encrypted = Buffer.concat([encrypted, cipher.final()]);
// let ivBuffer = Buffer.from(iv, 'utf8');
// let allBuffer = Buffer.concat([ivBuffer, encrypted]);
// return allBuffer.toString('base64');
// },
/**
* AES解密
* @param string word
* @param string key
* @returns string
*/
Decrypt(word = '', key = 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) {
// 将 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
},
}
export default EncAndDec