From 765145a357f9c3b8be82dcc4fc773a54054ab171 Mon Sep 17 00:00:00 2001 From: dgflash Date: Thu, 4 Sep 2025 10:53:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E5=AD=98=E5=82=A8=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/storage/StorageSecuritySimple.ts | 140 ++++++++++++++++-- assets/core/utils/MathUtil.ts | 2 +- assets/core/utils/PlatformUtil.ts | 1 - assets/core/utils/StringUtil.ts | 10 +- 4 files changed, 131 insertions(+), 22 deletions(-) diff --git a/assets/core/common/storage/StorageSecuritySimple.ts b/assets/core/common/storage/StorageSecuritySimple.ts index ead0054..f9aad78 100644 --- a/assets/core/common/storage/StorageSecuritySimple.ts +++ b/assets/core/common/storage/StorageSecuritySimple.ts @@ -1,15 +1,15 @@ -import { oops } from "../../Oops"; -import { IStorageSecurity } from "./StorageManager"; +import { oops } from '../../Oops'; +import { IStorageSecurity } from './StorageManager'; -/** +/** * 本地存储加密 * 优点: * 1、代码体积小 - * 2、不依赖第三方库,使用这套方案可删除 + * 2、不依赖第三方库,使用这套方案可删除 * StorageSecurityCrypto.ts * EncryptUtil.ts * package.json 中的crypto依赖减小包体 - * + * * 缺点: * 1、加密强度小 */ @@ -22,20 +22,130 @@ export class StorageSecuritySimple implements IStorageSecurity { this.secretkey = key + iv; } - encrypt(str: string): string { - let er = ''; - for (let i = 0; i < str.length; i++) { - er += String.fromCharCode(str.charCodeAt(i) ^ this.secretkey.charCodeAt(i % this.secretkey.length)); + /** + * 加密字符串 + */ + encrypt(data: string): string { + if (!data) return ''; + + // 先进行异或加密 + const xorEncrypted = this.xorEncrypt(data); + + // 然后进行 Base64 编码 + return this.base64Encode(xorEncrypted); + } + + /** 异或加密 */ + private xorEncrypt(data: string): string { + let result = ''; + for (let i = 0; i < data.length; i++) { + const keyChar = this.secretkey.charCodeAt(i % this.secretkey.length); + const dataChar = data.charCodeAt(i); + result += String.fromCharCode(dataChar ^ keyChar); + } + return result; + } + + /** 解密字符串 */ + decrypt(encryptedData: string): string { + if (!encryptedData) return ''; + + // 先进行 Base64 解码 + const base64Decoded = this.base64Decode(encryptedData); + + // 然后进行异或解密 + return this.xorDecrypt(base64Decoded); + } + + /** 异或解密 */ + private xorDecrypt(encryptedData: string): string { + return this.xorEncrypt(encryptedData); // 异或操作是可逆的 + } + + /** + * 安全的 Base64 编码 + */ + private base64Encode(data: string): string { + // 使用浏览器或 Node.js 的 Base64 编码 + if (typeof btoa === 'function') { + return btoa(data); + } + else if (typeof Buffer !== 'undefined') { + return Buffer.from(data).toString('base64'); + } + else { + // 回退到纯 JavaScript 实现 + return this.fallbackBase64Encode(data); + } + } + + /** + * 安全的 Base64 解码 + */ + private base64Decode(base64String: string): string { + // 使用浏览器或 Node.js 的 Base64 解码 + if (typeof atob === 'function') { + return atob(base64String); + } + else if (typeof Buffer !== 'undefined') { + return Buffer.from(base64String, 'base64').toString('utf8'); + } + else { + // 回退到纯 JavaScript 实现 + return this.fallbackBase64Decode(base64String); + } + } + + /** + * 回退的 Base64 编码实现 + */ + private fallbackBase64Encode(data: string): string { + const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + let output = ''; + let i = 0; + + while (i < data.length) { + const byte1 = data.charCodeAt(i++); + const byte2 = data.charCodeAt(i++); + const byte3 = data.charCodeAt(i++); + + const enc1 = byte1 >> 2; + const enc2 = ((byte1 & 3) << 4) | (byte2 >> 4); + const enc3 = isNaN(byte2) ? 64 : ((byte2 & 15) << 2) | (byte3 >> 6); + const enc4 = isNaN(byte3) ? 64 : byte3 & 63; + + output += chars.charAt(enc1) + chars.charAt(enc2) + chars.charAt(enc3) + chars.charAt(enc4); } - return er; + + return output; } - decrypt(str: string): string { - let dr = ''; - for (let i = 0; i < str.length; i++) { - dr += String.fromCharCode(str.charCodeAt(i) ^ this.secretkey.charCodeAt(i % this.secretkey.length)); + /** + * 回退的 Base64 解码实现 + */ + private fallbackBase64Decode(base64String: string): string { + const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + let output = ''; + let i = 0; + + base64String = base64String.replace(/[^A-Za-z0-9+/]/g, ''); + + while (i < base64String.length) { + const enc1 = chars.indexOf(base64String.charAt(i++)); + const enc2 = chars.indexOf(base64String.charAt(i++)); + const enc3 = chars.indexOf(base64String.charAt(i++)); + const enc4 = chars.indexOf(base64String.charAt(i++)); + + const byte1 = (enc1 << 2) | (enc2 >> 4); + const byte2 = ((enc2 & 15) << 4) | (enc3 >> 2); + const byte3 = ((enc3 & 3) << 6) | enc4; + + output += String.fromCharCode(byte1); + if (enc3 !== 64) output += String.fromCharCode(byte2); + if (enc4 !== 64) output += String.fromCharCode(byte3); } - return dr; + + return output; } encryptKey(str: string): string { diff --git a/assets/core/utils/MathUtil.ts b/assets/core/utils/MathUtil.ts index 819fad3..de8e6ca 100644 --- a/assets/core/utils/MathUtil.ts +++ b/assets/core/utils/MathUtil.ts @@ -127,4 +127,4 @@ export class MathUtil { static probability(value: number) { return Math.random() < value; } -} +} \ No newline at end of file diff --git a/assets/core/utils/PlatformUtil.ts b/assets/core/utils/PlatformUtil.ts index 56cef59..d9e2273 100644 --- a/assets/core/utils/PlatformUtil.ts +++ b/assets/core/utils/PlatformUtil.ts @@ -29,7 +29,6 @@ export class PlatformUtil { static async copyText(text: string) { if (sys.isNative) { native.copyTextToClipboard(text); - } else { await navigator.clipboard.writeText(text) diff --git a/assets/core/utils/StringUtil.ts b/assets/core/utils/StringUtil.ts index bf1e6e8..cfd43a1 100644 --- a/assets/core/utils/StringUtil.ts +++ b/assets/core/utils/StringUtil.ts @@ -146,16 +146,16 @@ export class StringUtil { /** * 是否为空 - * @param str + * @param str 字符串 */ - public static IsEmpty(str: string): boolean { + static isEmpty(str: string): boolean { return str == null || str == undefined || str.length == 0; } /** * 参数替换 - * @param str - * @param rest + * @param str 字符串 + * @param rest 参数 * * @example * @@ -164,7 +164,7 @@ export class StringUtil { * * "here is some info '15.4' and true" */ - public static substitute(str: string, ...rest: any[]): string { + static substitute(str: string, ...rest: any[]): string { if (str == null) return ''; let len: number = rest.length; -- Gitee