/* * @Author: gaorf30153 gaorf30153@hundsun.com * @Date: 2024-06-20 09:16:20 * @LastEditors: gaorf30153 gaorf30153@hundsun.com * @LastEditTime: 2024-06-24 11:20:29 * @FilePath: \wxminipro\plugin\crypto\aesutils.js * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ import { sm4 as SM4 } from "../gm-crypt/index.js" import { configObj } from "../config.js" import CryptoJS from "./crypto-js.js" class Crypt { constructor() { const key = configObj.key this.sm4Util = new SM4({ key, mode: "ecb", cipherType: "base64", }) } encrypt(data) { return this.sm4Util.encrypt(data) } decrypt(data) { return this.sm4Util.decrypt(data) } } export let cryptUtil = new Crypt() /** * 加密(依赖aes.js) * @param word 加密的字符串 * @returns {*} */ export function encrypt(word, encryptKey) { if (encryptKey.length > 16) { encryptKey = encryptKey.substring(0, 16) } var key = CryptoJS.enc.Utf8.parse(encryptKey) var srcs = CryptoJS.enc.Utf8.parse(word) var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) return encrypted.toString() } /** * 解密 * @param word 解密的字符串 * @returns {*} */ export function decrypt(word, encryptKey) { if (encryptKey.length > 16) { encryptKey = encryptKey.substring(0, 16) } var key = CryptoJS.enc.Utf8.parse(encryptKey) var decrypts = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) var minWen = CryptoJS.enc.Utf8.stringify(decrypts).toString() return minWen }