123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- /**
- * 数字加密与解密工具
- * 提供简单的加密解密功能
- */
- // 密钥(可以根据项目需要修改)
- const SECRET_KEY = 'cuteverse_moenova_secret_key_2023';
- /**
- * 对数字进行简单加密
- * @param {Number} num - 要加密的数字
- * @returns {String} - 加密后的字符串
- */
- export function encryptNumber(num) {
- if (typeof num !== 'number') {
- throw new Error('加密失败:输入必须是数字');
- }
-
- try {
- // 转换为字符串
- const strNum = num.toString();
-
- // 简单的凯撒加密
- let encrypted = '';
- for (let i = 0; i < strNum.length; i++) {
- const char = strNum.charAt(i);
- if (/\d/.test(char)) {
- // 对数字进行偏移
- const digit = parseInt(char);
- const shiftedDigit = (digit + 5) % 10;
- encrypted += shiftedDigit;
- } else {
- // 非数字字符保持不变
- encrypted += char;
- }
- }
-
- // Base64编码
- const base64 = btoa(encrypted + '|' + generateChecksum(encrypted));
-
- return base64;
- } catch (error) {
- console.error('加密过程出错:', error);
- return '';
- }
- }
- /**
- * 解密加密后的数字字符串
- * @param {String} encryptedStr - 加密后的字符串
- * @returns {Number|null} - 解密后的数字,解密失败返回null
- */
- export function decryptNumber(encryptedStr) {
- if (!encryptedStr) {
- return null;
- }
-
- try {
- // Base64解码
- const decoded = atob(encryptedStr);
-
- // 分离数据和校验和
- const parts = decoded.split('|');
- if (parts.length !== 2) {
- return null;
- }
-
- const encryptedData = parts[0];
- const checksum = parts[1];
-
- // 验证校验和
- if (generateChecksum(encryptedData) !== checksum) {
- console.warn('校验和不匹配,数据可能被篡改');
- return null;
- }
-
- // 反向凯撒解密
- let decrypted = '';
- for (let i = 0; i < encryptedData.length; i++) {
- const char = encryptedData.charAt(i);
- if (/\d/.test(char)) {
- // 对数字进行反向偏移
- const digit = parseInt(char);
- const shiftedDigit = (digit + 5) % 10; // +5 而不是 -5,因为 (d+5)%10 的逆运算是 (d+5)%10
- decrypted += shiftedDigit;
- } else {
- // 非数字字符保持不变
- decrypted += char;
- }
- }
-
- return parseInt(decrypted);
- } catch (error) {
- console.error('解密过程出错:', error);
- return null;
- }
- }
- /**
- * 批量加密数字数组
- * @param {Array<Number>} numbers - 要加密的数字数组
- * @returns {Array<String>} - 加密后的字符串数组
- */
- export function encryptNumbers(numbers) {
- if (!Array.isArray(numbers)) {
- return [];
- }
-
- return numbers.map(num => encryptNumber(num));
- }
- /**
- * 批量解密字符串数组
- * @param {Array<String>} encryptedStrings - 加密后的字符串数组
- * @returns {Array<Number>} - 解密后的数字数组
- */
- export function decryptNumbers(encryptedStrings) {
- if (!Array.isArray(encryptedStrings)) {
- return [];
- }
-
- return encryptedStrings
- .map(str => decryptNumber(str))
- .filter(num => num !== null);
- }
- /**
- * 加密用户ID和其他敏感数字信息
- * @param {Number|String} userId - 用户ID或其他敏感数字信息
- * @returns {String} - 加密后的字符串
- */
- export function encryptUserId(userId) {
- // 确保输入是数字
- const numId = parseInt(userId);
- if (isNaN(numId)) {
- throw new Error('用户ID必须是数字');
- }
-
- // 生成随机盐值
- const salt = Math.floor(Math.random() * 10000);
-
- // 混合加密
- const mixed = numId * 10000 + salt;
- const encrypted = encryptNumber(mixed);
-
- return encrypted;
- }
- /**
- * 解密用户ID
- * @param {String} encryptedUserId - 加密后的用户ID
- * @returns {Number|null} - 解密后的用户ID,解密失败返回null
- */
- export function decryptUserId(encryptedUserId) {
- const decrypted = decryptNumber(encryptedUserId);
- if (decrypted === null) {
- return null;
- }
-
- // 移除随机盐值
- return Math.floor(decrypted / 10000);
- }
- /**
- * 生成数据的校验和
- * @param {String} data - 要生成校验和的数据
- * @returns {String} - 校验和
- * @private
- */
- function generateChecksum(data) {
- let sum = 0;
- for (let i = 0; i < data.length; i++) {
- sum += data.charCodeAt(i);
- }
- return sum.toString(16);
- }
- /**
- * 检验数据是否被篡改
- * @param {String} data - 原始数据
- * @param {String} checksum - 校验和
- * @returns {Boolean} - 数据是否完整
- */
- export function verifyDataIntegrity(data, checksum) {
- return generateChecksum(data) === checksum;
- }
- export default {
- encryptNumber,
- decryptNumber,
- encryptNumbers,
- decryptNumbers,
- encryptUserId,
- decryptUserId,
- verifyDataIntegrity
- }
|