TODO:可信代码库-接口响应参数解密

This commit is contained in:
付民康
2025-06-26 18:02:06 +08:00
parent aab5aae5d9
commit 8dcd19412d
3 changed files with 69 additions and 3 deletions

View File

@@ -27,7 +27,7 @@
"@vueuse/core": "^10.2.1",
"axios": "^1.6.8",
"canvas-confetti": "^1.9.2",
"crypto-js": "^4.1.1",
"crypto-js": "^4.2.0",
"dayjs": "^1.11.13",
"devui-theme": "^0.0.7",
"dompurify": "^3.0.5",

View File

@@ -11,6 +11,67 @@ import { useAccountStore } from '@/stores/user';
const handleError = dealWarning();
import CryptoJS from 'crypto-js'; // 引入加密库
// 加密配置
const sKey = CryptoJS.enc.Utf8.parse('mXzfCSPBKmEA8aLq');
const iv = CryptoJS.enc.Utf8.parse('tbeJJLC6dZQXXtWr');
/**
* 解密函数
* @param text 待解密的Base64字符串
*/
const $Decrypt = (text) => {
debugger
if (!text) return text;
try {
// 处理Base64编码
let src = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Base64.parse(text));
// AES-CBC-PKCS7解密
let bytes = CryptoJS.AES.decrypt(src, sKey, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
let originalText = bytes.toString(CryptoJS.enc.Utf8);
// 尝试解析为JSON对象
return JSON.parse(originalText);
} catch (e) {
console.error('解密失败:', e);
return text; // 解密失败返回原始数据
}
};
/**
* 加密函数
* @param data 待加密的对象或字符串
*/
const $Encrypt = (data) => {
if (!data) return data;
try {
// 将数据转换为JSON字符串
const jsonData = typeof data === 'string' ? data : JSON.stringify(data);
// AES-CBC-PKCS7加密
const encrypted = CryptoJS.AES.encrypt(jsonData, sKey, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
// 转换为Base64字符串
return encrypted.toString();
} catch (e) {
console.error('加密失败:', e);
return data; // 加密失败返回原始数据
}
};
/** 设置用户中心相关api迁移时的api前缀 */
const setPassportPrefix = (url: string, method: string) => {
const prefix = (import.meta as any).env.VITE_PASSPORT_PREFIX;
@@ -45,6 +106,10 @@ export const baseDevURL = (import.meta as any).env.VITE_DEV_API_HOST;
type CustomConfigs = {
// 是否使用自定义的错误处理
customError?: boolean;
// 是否需要加密请求数据
needEncrypt?: boolean;
// 是否需要解密响应数据
needDecrypt?: boolean;
};
interface PendingTask {
@@ -135,6 +200,7 @@ const proxyService = (params: any, customConfigs?: CustomConfigs) => {
// response 拦截器
service.interceptors.response.use(
(response) => {
// response.data.data = $Decrypt('OTNNwNE3Q6oeb4diUBcOiA==')
return response;
},
async (error) => {

View File

@@ -2092,9 +2092,9 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.2:
shebang-command "^2.0.0"
which "^2.0.1"
crypto-js@^4.1.1:
crypto-js@^4.2.0:
version "4.2.0"
resolved "https://registry.npmjs.org/crypto-js/-/crypto-js-4.2.0.tgz"
resolved "https://mirrors.cloud.tencent.com/npm/crypto-js/-/crypto-js-4.2.0.tgz"
integrity sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q==
cssesc@^3.0.0: