Ver código fonte

取消两次加密修改

cyy 1 ano atrás
pai
commit
71390844ac

+ 1 - 2
src/utils/common.js

@@ -1,6 +1,6 @@
 // 通用工具类,定义通用函数及常用接口
 import { mapValues } from 'lodash'
-import { encryptByAes, decryptByAes } from '@/utils/encrypt'
+import { encryptByAes } from '@/utils/encrypt'
 // import { preview } from '@/business/platform/form/utils/custom/preview'
 import request from './joinCURD'
 import getRequest from './general'
@@ -221,7 +221,6 @@ export default {
   getNextIdByAlias,
   decode,
   encryptByAes,
-  decryptByAes,
   downloadByBlob,
   sendMsg,
   saveNews,

+ 19 - 87
src/utils/encrypt.js

@@ -1,97 +1,29 @@
 import CryptoJS from 'crypto-js'
-const key1 = CryptoJS.enc.Utf8.parse('dmngJmmO+9GMw+tu')
-// const iv1 = CryptoJS.enc.Utf8.parse('sanXyqhk8+U7LPP4')
-const key2 = CryptoJS.enc.Utf8.parse('49PBou+TREIOzSHj')
-// const iv2 = CryptoJS.enc.Utf8.parse('5lDsNRe&UduJ97uS')
 
-// 固定IV 用于密码、get请求参数加密
-const defaultIv = CryptoJS.enc.Utf8.parse('5lDsNRe&UduJ97uS')
-
-// 动态IV生成器
-export const generateDynamicIV = () => {
-  // 1. 获取当前时间戳(8字节,64位)
-  const timestamp = Date.now()
-  // 将时间戳转换为8字节的WordArray(大端序)
-  const timestampBytes = CryptoJS.enc.Hex.parse(
-    timestamp.toString(16).padStart(16, '0')
-  )
-
-  // 2. 生成8字节随机数
-  const randomBytes = CryptoJS.lib.WordArray.random(8)
-
-  // 3. 构造IV(16字节)
-  const ivWords = [
-    // 前4字节:随机数的高4字节
-    randomBytes.words[0],
-    // 接下来4字节:时间戳的高4字节
-    timestampBytes.words[0],
-    // 接下来4字节:随机数的低4字节
-    randomBytes.words[1],
-    // 最后4字节:时间戳的低4字节
-    timestampBytes.words[1]
-  ]
-
-  return CryptoJS.lib.WordArray.create(ivWords, 16)
-}
+const key = CryptoJS.enc.Utf8.parse('dmngJmmO+9GMw+tu')
+const iv = CryptoJS.enc.Utf8.parse('sanXyqhk8+U7LPP4')
 
 // AES加密
-export const encryptByAes = (params, type = 'normal') => {
+export const encryptByAes = pwd => {
   let encrypted = ''
-  const key = type === 'normal' ? key1 : key2
-  const iv = type === 'normal' ? generateDynamicIV() : defaultIv
-  if (typeof params === 'string' || typeof params === 'object') {
-    const data = typeof params === 'string' ? params : JSON.stringify(params)
+  if (typeof pwd === 'string') {
+    const srcs = CryptoJS.enc.Utf8.parse(pwd)
+    const options = {
+      iv: iv,
+      mode: CryptoJS.mode.CBC, // 使用CBC模式
+      padding: CryptoJS.pad.Pkcs7 // 使用PKCS7填充
+    }
+    encrypted = CryptoJS.AES.encrypt(srcs, key, options)
+  } else if (typeof pwd === 'object') {
+    const data = JSON.stringify(pwd)
     const srcs = CryptoJS.enc.Utf8.parse(data)
-    encrypted = CryptoJS.AES.encrypt(srcs, key, {
+    const options = {
       iv: iv,
-      mode: CryptoJS.mode.CBC,
-      padding: CryptoJS.pad.Pkcs7
-    })
-  }
-
-  return type === 'normal' ? {
-    ciphertext: encrypted.ciphertext.toString(CryptoJS.enc.Base64),
-    iv: iv.toString(CryptoJS.enc.Base64)
-  } : encrypted.ciphertext.toString(CryptoJS.enc.Base64)
-}
-
-const isValidBase64 = (str) => {
-  try {
-    return btoa(atob(str)) === str
-  } catch (e) {
-    return false
-  }
-}
-
-export const decryptByAes = (encryptedText, ivBase64 = null, type = 'normal') => {
-  const key = type === 'normal' ? key1 : key2
-
-  if (!isValidBase64(ivBase64)) {
-    console.error('ivBase64不是有效的 Base64 字符串')
-    return ''
-  }
-  // 解析动态IV
-  const iv = CryptoJS.enc.Base64.parse(ivBase64)
-
-  const options = {
-    iv: iv,
-    mode: CryptoJS.mode.CBC,
-    padding: CryptoJS.pad.Pkcs7
-  }
-
-  try {
-    const encryptedData = CryptoJS.enc.Base64.parse(encryptedText)
-    const decryptedData = CryptoJS.AES.decrypt(
-      { ciphertext: encryptedData },
-      key,
-      options
-    )
-
-    // 返回UTF-8解码的明文(去除首尾空格)
-    return decryptedData.toString(CryptoJS.enc.Utf8).trim()
-  } catch (error) {
-    console.error('AES解密失败:', error)
-    return ''
+      mode: CryptoJS.mode.CBC, // 使用CBC模式
+      padding: CryptoJS.pad.Pkcs7 // 使用PKCS7填充
+    }
+    encrypted = CryptoJS.AES.encrypt(srcs, key, options)
   }
+  return encrypted.ciphertext.toString(CryptoJS.enc.Base64)
 }
 

+ 1 - 1
src/utils/joinCURD.js

@@ -46,7 +46,7 @@ const dealData = (args, type) => {
   const data = typeof args === 'object' ? replaceNullWithEmpty(args) : args
   const plaintext = SHOW_PLAINTEXT ? { plaintext: data } : {}
   const res = {
-    ...encryptByAes(data),
+    ciphertext: encryptByAes(data),
     ...plaintext
   }
   return JSON.stringify(res)

+ 1 - 1
src/utils/request.js

@@ -93,7 +93,7 @@ service.interceptors.request.use(async config => {
   if (config.method.toUpperCase() === 'GET') {
     if (ENCRYPT_GET_PARAMS) {
       config.params = {
-        _p: Utils.isNotEmpty(config.params) ? encryptByAes(JSON.stringify(config.params), 'get') : undefined,
+        _p: Utils.isNotEmpty(config.params) ? encryptByAes(JSON.stringify(config.params)) : undefined,
         _t: Date.parse(new Date()) / 1000
       }
     } else {

+ 3 - 10
src/views/platform/my/change-password.vue

@@ -68,7 +68,7 @@ import navbar from '@/mixins/navbar'
 import defaultImage from '@/assets/images/logo/lc.png'
 import { mapState } from 'vuex'
 import { getFile } from '@/utils/image'
-import { encryptByAes } from '@/utils/encrypt'
+// import { encryptByAes } from '@/utils/encrypt'
 
 export default {
   mixins: [navbar],
@@ -106,15 +106,8 @@ export default {
     updatePassWord() {
       // console.log(this.form, 'this.form')
       // this.form['userIds'] = [this.$store.getters.userId]
-      const { primitivePassword, repeatPassword, newPassword } = this.form
-
-      const params = {
-        userIds: [this.$store.getters.userId],
-        primitivePassword: primitivePassword ? encryptByAes(primitivePassword, 'pwd') : '',
-        repeatPassword: encryptByAes(repeatPassword, 'pwd'),
-        newPassword: encryptByAes(newPassword, 'pwd')
-      }
-      changePassword(params).then(response => {
+      this.form['userIds'] = [this.$store.getters.userId]
+      changePassword(this.form).then(response => {
         this.$notify({
           type: 'success',
           message: this.$t('platform.my.change-password.success')

+ 3 - 4
src/views/system/login/index.vue

@@ -66,7 +66,7 @@ import { mapActions } from 'vuex'
 import Utils from '@/utils/util'
 import '@/assets/styles/pages/login.scss'
 import SwitchEnvironment from '@/views/system/switch-environment'
-import { encryptByAes } from '@/utils/encrypt'
+// import { encryptByAes } from '@/utils/encrypt'
 
 const loginForm = process.env.NODE_ENV === 'development'
   ? {
@@ -211,10 +211,9 @@ export default {
     },
     handleLogin() {
       // eslint-disable-next-line no-undef
-      const params = structuredClone(this.loginForm)
-      params.password = encryptByAes(this.loginForm.password, 'pwd')
+
       this.login({
-        form: params
+        form: this.loginForm
       }).then(() => {
         // let path = '/'
         // if (this.redirect) {