华西海圻ELN前端工程
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

  1. const CRYPTOJSKEY = '7LrjnqHv2mS5Qr5s'
  2. import CryptoJS from 'crypto-js'
  3. export function encrypt(plaintText) {
  4. var plaintText = plaintText
  5. var options = {
  6. mode: CryptoJS.mode.ECB,
  7. padding: CryptoJS.pad.Pkcs7
  8. }
  9. var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY)
  10. var encryptedData = CryptoJS.AES.encrypt(plaintText, key, options)
  11. var encryptedBase64Str = encryptedData.toString().replace(/\//g, '_')
  12. encryptedBase64Str = encryptedBase64Str.replace(/\+/g, '-')
  13. return encryptedBase64Str
  14. }
  15. export function decrypt(encryptedText) {
  16. // 还原 URL 安全字符
  17. let encryptedBase64Str = encryptedText
  18. .replace(/_/g, '/') // 还原 _
  19. .replace(/-/g, '+') // 还原 -
  20. // 补充末尾的 =(如果需要)
  21. const mod4 = encryptedBase64Str.length % 4
  22. if (mod4) {
  23. encryptedBase64Str += '='.repeat(4 - mod4)
  24. }
  25. try {
  26. const key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY)
  27. const decrypt = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
  28. mode: CryptoJS.mode.ECB,
  29. padding: CryptoJS.pad.Pkcs7
  30. })
  31. return CryptoJS.enc.Utf8.stringify(decrypt).toString()
  32. } catch (e) {
  33. console.error('解密失败:', e)
  34. return null
  35. }
  36. }