海口龙华志愿者后端管理页面
zhouxiantao
8 days ago 4def10e6567cf651ae333a2a65c497e717c06403
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * AES加密工具
 * @author 韩天尊
 * @date 2024
 * 
 * 加密方式:AES/ECB/PKCS5Padding
 * 密钥:rSc4Mq8Ywu1NuNJU
 */
 
import CryptoJS from 'crypto-js';
 
// AES加密密钥
const AES_KEY = 'rSc4Mq8Ywu1NuNJU';
 
/**
 * AES加密
 * @param {string} content 待加密内容
 * @returns {string} 加密后的Base64字符串
 */
export function aesEncrypt(content) {
  if (!content) {
    console.warn('AES encrypt: the content is null!');
    return null;
  }
 
  try {
    // 使用ECB模式,PKCS5Padding填充方式
    const key = CryptoJS.enc.Utf8.parse(AES_KEY);
    const encrypted = CryptoJS.AES.encrypt(content, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    
    // 返回Base64编码的字符串
    return encrypted.toString();
  } catch (error) {
    console.error('AES encrypt exception:', error.message);
    throw new Error('加密失败');
  }
}
 
/**
 * AES解密
 * @param {string} content 待解密内容(Base64字符串)
 * @returns {string} 解密后的原始字符串
 */
export function aesDecrypt(content) {
  if (!content) {
    console.warn('AES decrypt: the content is null!');
    return null;
  }
 
  try {
    const key = CryptoJS.enc.Utf8.parse(AES_KEY);
    const decrypted = CryptoJS.AES.decrypt(content, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    
    return decrypted.toString(CryptoJS.enc.Utf8);
  } catch (error) {
    console.error('AES decrypt exception:', error.message);
    return null;
  }
}
 
/**
 * 生成Sign请求头值
 * 格式:对"zyz-时间戳"进行AES加密
 * @returns {string} 加密后的Sign值
 */
export function generateSign() {
  const timestamp = Date.now();
  const content = `${timestamp}_zyz`;
  return aesEncrypt(content);
}