Files
chenxing 19e1bbc9d7
build / build (push) Successful in 1m8s
设置界面更新
2026-10-08 10:08:22 +08:00

48 lines
1.6 KiB
JavaScript

(function (global) {
/* ---------- 1. 默认 16 字节密钥 ---------- */
const DEFAULT_KEY_WA = CryptoJS.MD5("bsch2025"); // WordArray(16 byte)
class BschCryptoUtil {
#aesKey;
constructor(key) {
// 支持外部传入字符串或 WordArray,默认用隐藏后的 MD5
this.#aesKey = key != null ? key : DEFAULT_KEY_WA;
}
encrypt(data) {
const message = typeof data === "string" ? data : JSON.stringify(data);
const encrypted = CryptoJS.AES.encrypt(message, this.#aesKey, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
return encrypted.toString(); // Base64
}
decrypt(cipherText) {
if (!cipherText) throw new Error("cipherText is empty");
const decrypted = CryptoJS.AES.decrypt(cipherText, this.#aesKey, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
const plain = decrypted.toString(CryptoJS.enc.Utf8);
if (!plain) throw new Error("decrypt failed");
try {
return JSON.parse(plain);
} catch (_) {
return plain;
}
}
static encrypt(data, key) {
return new BschCryptoUtil(key).encrypt(data);
}
static decrypt(cipherText, key) {
return new BschCryptoUtil(key).decrypt(cipherText);
}
}
/* ---------- 6. 暴露到全局 ---------- */
global.BschCryptoUtil = BschCryptoUtil;
})(typeof window !== "undefined" ? window : this);