|
| 1 | +// 引入必要模块 |
| 2 | +import CryptoJS from 'crypto-js'; |
| 3 | +import pako from 'pako'; |
| 4 | +import { Buffer } from 'buffer'; |
| 5 | + |
| 6 | +/** |
| 7 | + * 随机生成加密和解密算法 |
| 8 | + * @returns {Object} - 包含加密和解密函数的对象 |
| 9 | + */ |
| 10 | +function createRandomEncryption() { |
| 11 | + // 可用的编码工具 |
| 12 | + const tools = [ |
| 13 | + { |
| 14 | + name: 'base64Encode', |
| 15 | + encode: (data) => Buffer.from(data).toString('base64'), |
| 16 | + decode: (data) => Buffer.from(data, 'base64').toString('utf-8') |
| 17 | + }, |
| 18 | + { |
| 19 | + name: 'aesEncrypt', |
| 20 | + encode: (data, key) => CryptoJS.AES.encrypt(data, key).toString(), |
| 21 | + decode: (data, key) => CryptoJS.AES.decrypt(data, key).toString(CryptoJS.enc.Utf8) |
| 22 | + }, |
| 23 | + { |
| 24 | + name: 'gzipCompress', |
| 25 | + encode: (data) => Buffer.from(pako.gzip(data)).toString('base64'), |
| 26 | + decode: (data) => pako.ungzip(Buffer.from(data, 'base64'), { to: 'string' }) |
| 27 | + }, |
| 28 | + { |
| 29 | + name: 'addRandomString', |
| 30 | + encode: (data) => { |
| 31 | + const length = Math.floor(Math.random() * 7) + 4; // 随机长度在4到10之间 |
| 32 | + const randomStr = Math.random().toString(36).substring(2, 2 + length); |
| 33 | + addRandomStringTracker.push({ value: randomStr, position: addRandomStringTracker.length }); |
| 34 | + return data + randomStr; |
| 35 | + }, |
| 36 | + decode: (data) => { |
| 37 | + const { value } = addRandomStringTracker.pop(); |
| 38 | + return data.slice(0, -value.length); |
| 39 | + } |
| 40 | + } |
| 41 | + ]; |
| 42 | + |
| 43 | + // 跟踪插入的随机字符串 |
| 44 | + const addRandomStringTracker = []; |
| 45 | + |
| 46 | + // 随机生成加密和解密的组合序列 |
| 47 | + const encodeSequence = Array.from({ length: Math.floor(Math.random() * tools.length) + 1 }, () => { |
| 48 | + return tools[Math.floor(Math.random() * tools.length)]; |
| 49 | + }); |
| 50 | + |
| 51 | + // 生成加密函数 |
| 52 | + const encrypt = (data, key = 'default_key') => { |
| 53 | + let encrypted = data; |
| 54 | + for (const tool of encodeSequence) { |
| 55 | + encrypted = tool.encode(encrypted, key); |
| 56 | + } |
| 57 | + return encrypted; |
| 58 | + }; |
| 59 | + |
| 60 | + // 生成解密函数 |
| 61 | + const decrypt = (data, key = 'default_key') => { |
| 62 | + let decrypted = data; |
| 63 | + for (const tool of encodeSequence.slice().reverse()) { |
| 64 | + decrypted = tool.decode(decrypted, key); |
| 65 | + } |
| 66 | + return decrypted; |
| 67 | + }; |
| 68 | + |
| 69 | + // 返回加解密函数 |
| 70 | + return { |
| 71 | + encrypt, |
| 72 | + decrypt, |
| 73 | + sequence: encodeSequence.map(tool => tool.name), |
| 74 | + randomStrings: addRandomStringTracker |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +// 示例使用 |
| 79 | +const { encrypt, decrypt, sequence, randomStrings } = createRandomEncryption(); |
| 80 | +const originalText = 'Hello, world!'; |
| 81 | +const key = 'my_secret_key'; |
| 82 | + |
| 83 | +const encryptedText = encrypt(originalText, key); |
| 84 | +console.log('加密算法顺序:', sequence); |
| 85 | +console.log('插入的随机字符串:', randomStrings.map(({ value, position }) => `位置: ${position}, 值: ${value}`)); |
| 86 | +console.log('加密后的文本:', encryptedText); |
| 87 | + |
| 88 | +const decryptedText = decrypt(encryptedText, key); |
| 89 | +console.log('解密后的文本:', decryptedText); |
0 commit comments