update 升级 依赖版本并替换废弃依赖 crypto-js
- 替换已废弃的 crypto-js 为 @noble/ciphers@2.4.0(零依赖、经过安全审计),
重写 src/utils/crypto.ts,密文格式保持 AES-256-ECB/PKCS7Padding + Base64,
与后端 Hutool AES 完全兼容,后端零改动(已通过 openssl 比对验证)
- 升级 @wangeditor-next/editor 5.7.16 -> 6.4.1、
@wangeditor-next/editor-for-vue 5.1.14 -> 6.4.1(版本严格对齐,
v6 唯一不兼容点为视频对齐 HTML 结构,导入时自动迁移)
- 升级 vue 3.5.40 -> 3.5.42、element-plus 2.14.3 -> 2.14.5、
vxe-table 4.20.8 -> 4.21.9、vxe-pc-ui 4.16.22 -> 4.18.9、
axios 1.18.1 -> 1.20.0、pinia 4.0.2 -> 4.0.3、vue-router 5.2.0 -> 5.3.1、
@vueuse/core 14.3.0 -> 14.4.0、highlight.js 11.11.1 -> 11.12.0、
vue-i18n 11.4.8 -> 11.4.10 及 unocss、sass、oxlint 等开发依赖
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
import * as CryptoJSModule from 'crypto-js';
|
||||
|
||||
const CryptoJS = ('default' in CryptoJSModule ? CryptoJSModule.default : CryptoJSModule) as typeof CryptoJSModule;
|
||||
import { ecb } from '@noble/ciphers/aes.js';
|
||||
|
||||
/**
|
||||
* 随机生成32位的字符串
|
||||
@@ -14,41 +12,58 @@ const generateRandomString = (): string => {
|
||||
.slice(0, 32);
|
||||
};
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
const bytesToBase64 = (bytes: Uint8Array): string => {
|
||||
let binary = '';
|
||||
for (const byte of bytes) {
|
||||
binary += String.fromCharCode(byte);
|
||||
}
|
||||
return btoa(binary);
|
||||
};
|
||||
|
||||
const base64ToBytes = (str: string): Uint8Array => {
|
||||
const binary = atob(str);
|
||||
const bytes = new Uint8Array(binary.length);
|
||||
for (let i = 0; i < binary.length; i++) {
|
||||
bytes[i] = binary.charCodeAt(i);
|
||||
}
|
||||
return bytes;
|
||||
};
|
||||
|
||||
/**
|
||||
* 随机生成aes 密钥
|
||||
* @returns {string}
|
||||
* @returns {Uint8Array}
|
||||
*/
|
||||
export const generateAesKey = (): CryptoJSModule.lib.WordArray => {
|
||||
return CryptoJS.enc.Utf8.parse(generateRandomString());
|
||||
export const generateAesKey = (): Uint8Array => {
|
||||
return encoder.encode(generateRandomString());
|
||||
};
|
||||
|
||||
/**
|
||||
* 加密base64
|
||||
* @returns {string}
|
||||
*/
|
||||
export const encryptBase64 = (str: CryptoJSModule.lib.WordArray): string => {
|
||||
return CryptoJS.enc.Base64.stringify(str);
|
||||
export const encryptBase64 = (bytes: Uint8Array): string => {
|
||||
return bytesToBase64(bytes);
|
||||
};
|
||||
|
||||
/**
|
||||
* 解密base64
|
||||
*/
|
||||
export const decryptBase64 = (str: string) => {
|
||||
return CryptoJS.enc.Base64.parse(str);
|
||||
export const decryptBase64 = (str: string): Uint8Array => {
|
||||
return base64ToBytes(str);
|
||||
};
|
||||
|
||||
/**
|
||||
* 使用密钥对数据进行加密
|
||||
* 使用密钥对数据进行加密 (AES/ECB/PKCS7Padding 与后端保持一致)
|
||||
* @param message
|
||||
* @param aesKey
|
||||
* @returns {string}
|
||||
*/
|
||||
export const encryptWithAes = (message: string, aesKey: CryptoJSModule.lib.WordArray): string => {
|
||||
const encrypted = CryptoJS.AES.encrypt(message, aesKey, {
|
||||
mode: CryptoJS.mode.ECB,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
});
|
||||
return encrypted.toString();
|
||||
export const encryptWithAes = (message: string, aesKey: Uint8Array): string => {
|
||||
const encrypted = ecb(aesKey).encrypt(encoder.encode(message));
|
||||
return bytesToBase64(encrypted);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -57,10 +72,7 @@ export const encryptWithAes = (message: string, aesKey: CryptoJSModule.lib.WordA
|
||||
* @param aesKey
|
||||
* @returns {string}
|
||||
*/
|
||||
export const decryptWithAes = (message: string, aesKey: CryptoJSModule.lib.WordArray): string => {
|
||||
const decrypted = CryptoJS.AES.decrypt(message, aesKey, {
|
||||
mode: CryptoJS.mode.ECB,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
});
|
||||
return decrypted.toString(CryptoJS.enc.Utf8);
|
||||
export const decryptWithAes = (message: string, aesKey: Uint8Array): string => {
|
||||
const decrypted = ecb(aesKey).decrypt(base64ToBytes(message));
|
||||
return decoder.decode(decrypted);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user