jiangping
2023-08-17 6365ab0a976afdd247742c9b3dca15deb3a7a7a1
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
import CryptoJS from 'crypto-js'
 
const KEY = process.env.VUE_APP_ENCRYPT_REQUEST_KEY
const IV = process.env.VUE_APP_ENCRYPT_REQUEST_IV
 
export default {
  /**
   * 加密
   *
   * @param plaintext 明文
   * @returns {string}
   */
  encrypt (plaintext) {
    const key = CryptoJS.enc.Utf8.parse(KEY)
    const secretData = CryptoJS.enc.Utf8.parse(plaintext)
    const encrypted = CryptoJS.AES.encrypt(
      secretData,
      key,
      {
        iv: CryptoJS.enc.Utf8.parse(IV),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      }
    )
    return encrypted.toString()
  },
  /**
   * 解密
   *
   * @param cipherText 密文
   * @returns {*}
   */
  decrypt (cipherText) {
    const key = CryptoJS.enc.Utf8.parse(KEY)
    const decrypt = CryptoJS.AES.decrypt(
      cipherText,
      key,
      {
        iv: CryptoJS.enc.Utf8.parse(IV),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      }
    )
    return CryptoJS.enc.Utf8.stringify(decrypt).toString()
  }
}