From 2dc4b863846eead0f3d91bac0e1b06ec1160adae Mon Sep 17 00:00:00 2001 From: binbin_pan Date: Thu, 23 May 2024 19:19:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=98=8E=E6=96=87=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sgzb-ui/src/views/system/config/index.vue | 32 +++++++++++++- sgzb-ui/src/views/system/user/index.vue | 53 ++++++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/sgzb-ui/src/views/system/config/index.vue b/sgzb-ui/src/views/system/config/index.vue index 1299d6cc..9e9bffa2 100644 --- a/sgzb-ui/src/views/system/config/index.vue +++ b/sgzb-ui/src/views/system/config/index.vue @@ -229,7 +229,8 @@ export default { configValue: [ { required: true, message: "参数键值不能为空", trigger: "blur" } ] - } + }, + secretKey: 'CCNWrpassWordKey' }; }, created() { @@ -240,6 +241,15 @@ export default { getList() { this.loading = true; listConfig(this.addDateRange(this.queryParams, this.dateRange)).then(response => { + response.rows.forEach(row => { + if (row.configKey == 'sys.user.initPassword') { + this.decryptData(row.configValue, this.secretKey).then(data => { + row.configValue = data; + }).catch(err => { + console.log('🚀 ~ decryptData ~ err:', err); + }); + } + }); this.configList = response.rows; this.total = response.total; this.loading = false; @@ -337,6 +347,26 @@ export default { refreshCache().then(() => { this.$modal.msgSuccess("刷新成功"); }); + }, + async decryptData(encryptedData, keyStr) { + const keyUint8 = new TextEncoder().encode(keyStr); + const encryptedBytes = Uint8Array.from(atob(encryptedData), c => c.charCodeAt(0)); + + const key = await crypto.subtle.importKey( + 'raw', + keyUint8, + { name: 'AES-CBC', length: 256 }, // 假设后端使用了CBC模式,需要调整为实际使用的模式 + false, + ['decrypt'] + ); + + const decryptedData = await crypto.subtle.decrypt( + { name: 'AES-CBC', iv: new Uint8Array(16) }, // 实际使用时需要正确的IV,这里仅为示例 + key, + encryptedBytes + ); + + return new TextDecoder().decode(decryptedData); } } }; diff --git a/sgzb-ui/src/views/system/user/index.vue b/sgzb-ui/src/views/system/user/index.vue index 751a0aa8..aa81628c 100644 --- a/sgzb-ui/src/views/system/user/index.vue +++ b/sgzb-ui/src/views/system/user/index.vue @@ -410,6 +410,7 @@ export default { }, ], }, + secretKey: 'CCNWrpassWordKey' } }, watch: { @@ -422,7 +423,12 @@ export default { this.getList() this.getDeptTree() this.getConfigKey('sys.user.initPassword').then((response) => { - this.initPassword = response.msg + this.decryptData(response.msg, this.secretKey).then((data) => { + console.log('🚀 ~ this.getConfigKey ~ data:', data); + this.initPassword = data + }).catch((error) => { + console.log('🚀 ~ this.getConfigKey ~ error:', error); + }) }) }, methods: { @@ -668,6 +674,51 @@ export default { submitFileForm() { this.$refs.upload.submit() }, + // 加密 + async encryptData(data, keyStr) { + const keyUint8 = new TextEncoder().encode(keyStr); + const key = await crypto.subtle.importKey( + 'raw', + keyUint8, + { name: 'AES-CBC', length: 256 }, + false, + ['encrypt'] + ); + + const iv = crypto.getRandomValues(new Uint8Array(16)); + const cipherTextBuffer = await crypto.subtle.encrypt( + { name: 'AES-CBC', iv }, + key, + new TextEncoder().encode(data) + ); + + const combined = new Uint8Array(iv.length + cipherTextBuffer.byteLength); + combined.set(iv, 0); + combined.set(new Uint8Array(cipherTextBuffer), iv.length); + + return btoa(String.fromCharCode.apply(null, combined)); + }, + // 解密 + async decryptData(encryptedData, keyStr) { + const keyUint8 = new TextEncoder().encode(keyStr); + const encryptedBytes = Uint8Array.from(atob(encryptedData), c => c.charCodeAt(0)); + + const key = await crypto.subtle.importKey( + 'raw', + keyUint8, + { name: 'AES-CBC', length: 256 }, // 假设后端使用了CBC模式,需要调整为实际使用的模式 + false, + ['decrypt'] + ); + + const decryptedData = await crypto.subtle.decrypt( + { name: 'AES-CBC', iv: new Uint8Array(16) }, // 实际使用时需要正确的IV,这里仅为示例 + key, + encryptedBytes + ); + + return new TextDecoder().decode(decryptedData); + } }, }