crypto.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * 数字加密与解密工具
  3. * 提供简单的加密解密功能
  4. */
  5. // 密钥(可以根据项目需要修改)
  6. const SECRET_KEY = 'cuteverse_moenova_secret_key_2023';
  7. /**
  8. * 对数字进行简单加密
  9. * @param {Number} num - 要加密的数字
  10. * @returns {String} - 加密后的字符串
  11. */
  12. export function encryptNumber(num) {
  13. if (typeof num !== 'number') {
  14. throw new Error('加密失败:输入必须是数字');
  15. }
  16. try {
  17. // 转换为字符串
  18. const strNum = num.toString();
  19. // 简单的凯撒加密
  20. let encrypted = '';
  21. for (let i = 0; i < strNum.length; i++) {
  22. const char = strNum.charAt(i);
  23. if (/\d/.test(char)) {
  24. // 对数字进行偏移
  25. const digit = parseInt(char);
  26. const shiftedDigit = (digit + 5) % 10;
  27. encrypted += shiftedDigit;
  28. } else {
  29. // 非数字字符保持不变
  30. encrypted += char;
  31. }
  32. }
  33. // Base64编码
  34. const base64 = btoa(encrypted + '|' + generateChecksum(encrypted));
  35. return base64;
  36. } catch (error) {
  37. console.error('加密过程出错:', error);
  38. return '';
  39. }
  40. }
  41. /**
  42. * 解密加密后的数字字符串
  43. * @param {String} encryptedStr - 加密后的字符串
  44. * @returns {Number|null} - 解密后的数字,解密失败返回null
  45. */
  46. export function decryptNumber(encryptedStr) {
  47. if (!encryptedStr) {
  48. return null;
  49. }
  50. try {
  51. // Base64解码
  52. const decoded = atob(encryptedStr);
  53. // 分离数据和校验和
  54. const parts = decoded.split('|');
  55. if (parts.length !== 2) {
  56. return null;
  57. }
  58. const encryptedData = parts[0];
  59. const checksum = parts[1];
  60. // 验证校验和
  61. if (generateChecksum(encryptedData) !== checksum) {
  62. console.warn('校验和不匹配,数据可能被篡改');
  63. return null;
  64. }
  65. // 反向凯撒解密
  66. let decrypted = '';
  67. for (let i = 0; i < encryptedData.length; i++) {
  68. const char = encryptedData.charAt(i);
  69. if (/\d/.test(char)) {
  70. // 对数字进行反向偏移
  71. const digit = parseInt(char);
  72. const shiftedDigit = (digit + 5) % 10; // +5 而不是 -5,因为 (d+5)%10 的逆运算是 (d+5)%10
  73. decrypted += shiftedDigit;
  74. } else {
  75. // 非数字字符保持不变
  76. decrypted += char;
  77. }
  78. }
  79. return parseInt(decrypted);
  80. } catch (error) {
  81. console.error('解密过程出错:', error);
  82. return null;
  83. }
  84. }
  85. /**
  86. * 批量加密数字数组
  87. * @param {Array<Number>} numbers - 要加密的数字数组
  88. * @returns {Array<String>} - 加密后的字符串数组
  89. */
  90. export function encryptNumbers(numbers) {
  91. if (!Array.isArray(numbers)) {
  92. return [];
  93. }
  94. return numbers.map(num => encryptNumber(num));
  95. }
  96. /**
  97. * 批量解密字符串数组
  98. * @param {Array<String>} encryptedStrings - 加密后的字符串数组
  99. * @returns {Array<Number>} - 解密后的数字数组
  100. */
  101. export function decryptNumbers(encryptedStrings) {
  102. if (!Array.isArray(encryptedStrings)) {
  103. return [];
  104. }
  105. return encryptedStrings
  106. .map(str => decryptNumber(str))
  107. .filter(num => num !== null);
  108. }
  109. /**
  110. * 加密用户ID和其他敏感数字信息
  111. * @param {Number|String} userId - 用户ID或其他敏感数字信息
  112. * @returns {String} - 加密后的字符串
  113. */
  114. export function encryptUserId(userId) {
  115. // 确保输入是数字
  116. const numId = parseInt(userId);
  117. if (isNaN(numId)) {
  118. throw new Error('用户ID必须是数字');
  119. }
  120. // 生成随机盐值
  121. const salt = Math.floor(Math.random() * 10000);
  122. // 混合加密
  123. const mixed = numId * 10000 + salt;
  124. const encrypted = encryptNumber(mixed);
  125. return encrypted;
  126. }
  127. /**
  128. * 解密用户ID
  129. * @param {String} encryptedUserId - 加密后的用户ID
  130. * @returns {Number|null} - 解密后的用户ID,解密失败返回null
  131. */
  132. export function decryptUserId(encryptedUserId) {
  133. const decrypted = decryptNumber(encryptedUserId);
  134. if (decrypted === null) {
  135. return null;
  136. }
  137. // 移除随机盐值
  138. return Math.floor(decrypted / 10000);
  139. }
  140. /**
  141. * 生成数据的校验和
  142. * @param {String} data - 要生成校验和的数据
  143. * @returns {String} - 校验和
  144. * @private
  145. */
  146. function generateChecksum(data) {
  147. let sum = 0;
  148. for (let i = 0; i < data.length; i++) {
  149. sum += data.charCodeAt(i);
  150. }
  151. return sum.toString(16);
  152. }
  153. /**
  154. * 检验数据是否被篡改
  155. * @param {String} data - 原始数据
  156. * @param {String} checksum - 校验和
  157. * @returns {Boolean} - 数据是否完整
  158. */
  159. export function verifyDataIntegrity(data, checksum) {
  160. return generateChecksum(data) === checksum;
  161. }
  162. export default {
  163. encryptNumber,
  164. decryptNumber,
  165. encryptNumbers,
  166. decryptNumbers,
  167. encryptUserId,
  168. decryptUserId,
  169. verifyDataIntegrity
  170. }