WXL
13 小时以前 3e280975ba21c9b311f3538788c220bdd70e16bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// utils/crypto.js
import CryptoJS from "crypto-js";
 
// ✅ 密钥(前后端必须一致)
const SECRET_KEY = "QfOpO2026@SecretKey#Aes256!00001";
const IV = "1234567890123456"; // 16位
console.log(SECRET_KEY);
 
/**
 * AES 加密
 */
export function encrypts(text) {
  const key = CryptoJS.enc.Utf8.parse(SECRET_KEY);
  const iv = CryptoJS.enc.Utf8.parse(IV);
 
  const encrypted = CryptoJS.AES.encrypt(text, key, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
 
  return encrypted.toString(); // Base64
}
 
/**
 * AES 解密(可选,用于调试)
 */
export function decrypt(cipherText) {
  const key = CryptoJS.enc.Utf8.parse(SECRET_KEY);
  const iv = CryptoJS.enc.Utf8.parse(IV);
 
  const decrypted = CryptoJS.AES.decrypt(cipherText, key, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
 
  return decrypted.toString(CryptoJS.enc.Utf8);
}