明文加密

This commit is contained in:
binbin_pan 2024-05-23 19:19:10 +08:00
parent c63284ee05
commit 2dc4b86384
2 changed files with 83 additions and 2 deletions

View File

@ -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);
}
}
};

View File

@ -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);
}
},
}
</script>