/**
|
* 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);
|
}
|