您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

aesutils.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * @Author: gaorf30153 gaorf30153@hundsun.com
  3. * @Date: 2024-06-20 09:16:20
  4. * @LastEditors: gaorf30153 gaorf30153@hundsun.com
  5. * @LastEditTime: 2024-06-24 11:20:29
  6. * @FilePath: \wxminipro\plugin\crypto\aesutils.js
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. import { sm4 as SM4 } from "../gm-crypt/index.js"
  10. import { configObj } from "../config.js"
  11. import CryptoJS from "./crypto-js.js"
  12. class Crypt {
  13. constructor() {
  14. const key = configObj.key
  15. this.sm4Util = new SM4({
  16. key,
  17. mode: "ecb",
  18. cipherType: "base64",
  19. })
  20. }
  21. encrypt(data) {
  22. return this.sm4Util.encrypt(data)
  23. }
  24. decrypt(data) {
  25. return this.sm4Util.decrypt(data)
  26. }
  27. }
  28. export let cryptUtil = new Crypt()
  29. /**
  30. * 加密(依赖aes.js)
  31. * @param word 加密的字符串
  32. * @returns {*}
  33. */
  34. export function encrypt(word, encryptKey) {
  35. if (encryptKey.length > 16) {
  36. encryptKey = encryptKey.substring(0, 16)
  37. }
  38. var key = CryptoJS.enc.Utf8.parse(encryptKey)
  39. var srcs = CryptoJS.enc.Utf8.parse(word)
  40. var encrypted = CryptoJS.AES.encrypt(srcs, key, {
  41. mode: CryptoJS.mode.ECB,
  42. padding: CryptoJS.pad.Pkcs7
  43. })
  44. return encrypted.toString()
  45. }
  46. /**
  47. * 解密
  48. * @param word 解密的字符串
  49. * @returns {*}
  50. */
  51. export function decrypt(word, encryptKey) {
  52. if (encryptKey.length > 16) {
  53. encryptKey = encryptKey.substring(0, 16)
  54. }
  55. var key = CryptoJS.enc.Utf8.parse(encryptKey)
  56. var decrypts = CryptoJS.AES.decrypt(word, key, {
  57. mode: CryptoJS.mode.ECB,
  58. padding: CryptoJS.pad.Pkcs7
  59. })
  60. var minWen = CryptoJS.enc.Utf8.stringify(decrypts).toString()
  61. return minWen
  62. }