🐛 修复加密失败的问题.

This commit is contained in:
lijiahangmax
2025-01-22 22:32:10 +08:00
parent aeb161a482
commit 4e5730f31f
7 changed files with 59 additions and 59 deletions

View File

@@ -2,6 +2,9 @@ import { JSEncrypt } from 'jsencrypt';
import { useCacheStore } from '@/store';
import { Message } from '@arco-design/web-vue';
// 分块大小
const BLOCK_SIZE = 100;
// 加密
export const encrypt = async (data: string | undefined): Promise<string | undefined> => {
// 为空直接返回
@@ -9,14 +12,32 @@ export const encrypt = async (data: string | undefined): Promise<string | undefi
return data;
}
// 获取公钥
const { encrypt } = await useCacheStore().loadSystemSetting();
const publicKey = (await useCacheStore().loadSystemSetting()).encrypt?.publicKey;
const encryptor = new JSEncrypt();
encryptor.setPublicKey(encrypt?.publicKey);
// 加密
const value = encryptor.encrypt(data);
if (value === false) {
encryptor.setPublicKey(publicKey);
try {
// 分块加密
const chunks = splitString(data);
const encryptedChunks = chunks.map(chunk => {
const encrypted = encryptor.encrypt(chunk);
if (encrypted === false) {
throw new Error();
}
return encrypted;
});
return encryptedChunks.join('|');
} catch (error) {
Message.error('数据加密失败');
throw new Error('数据加密失败');
}
return value;
};
// 分割字符串
const splitString = (str: string): string[] => {
const chunks: string[] = [];
for (let i = 0; i < str.length; i += BLOCK_SIZE) {
chunks.push(str.slice(i, i + BLOCK_SIZE));
}
return chunks;
};