crypto.ts 1007 B

123456789101112131415161718192021222324252627282930
  1. import * as CryptoJS from 'crypto-ts'
  2. export default {
  3. AES_KEY: 'mt',
  4. encode(data: string): string {
  5. if (typeof data !== 'string') return ''
  6. // 加密
  7. const key = CryptoJS.enc.Utf8.parse(this.AES_KEY)
  8. const str = JSON.stringify(data)
  9. const encryptedData = CryptoJS.AES.encrypt(str, key, {
  10. mode: CryptoJS.mode.ECB,
  11. padding: CryptoJS.pad.PKCS7,
  12. iv: CryptoJS.enc.Utf8.parse(this.AES_KEY)
  13. })
  14. return encryptedData.toString()
  15. },
  16. // 解密
  17. decode(data: string): string {
  18. if (typeof data !== 'string') return ''
  19. const encryptedHexStr = CryptoJS.enc.Utf8.parse(data)
  20. const encryptedBase64Str = CryptoJS.enc.Utf8.stringify(encryptedHexStr)
  21. const key = CryptoJS.enc.Utf8.parse(this.AES_KEY)
  22. const decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
  23. mode: CryptoJS.mode.ECB,
  24. padding: CryptoJS.pad.PKCS7,
  25. iv: CryptoJS.enc.Utf8.parse(this.AES_KEY)
  26. })
  27. return decryptedData.toString(CryptoJS.enc.Utf8)
  28. }
  29. }