1.安装JSEncrypt
npm install jsencrypt
2.加密方法
//加密算法
import { JSEncrypt } from 'jsencrypt';
// 加密
function encryptText(text) {
const publicKey='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCh5Nk2GLiyQFMIU+h3OEA4UeFbu3dCH5sjd/sLTxxvwjXq7JLqJbt2rCIdzpAXOi4jL+FRGQnHaxUlHUBZsojnCcHvhrz2knV6rXNogt0emL7f7ZMRo8IsQGV8mlKIC9xLnlOQQdRNUssmrROrCG99wpTRRNZjOmLvkcoXdeuaCQIDAQAB'
const instance = new JSEncrypt();
instance.setPublicKey(publicKey);
return instance.encrypt(text);
}
// 解密
function decryptText(text) {
const instance = new JSEncrypt();
instance.setPrivateKey(privateKey);
return instance.decrypt(text);
}
export {
encryptText,
decryptText,
}
3.登录页面,密码加密
引用util.js
const onSubmit = () => {
formRef.value.validate((valid)=>{
if(!valid){
return false
}
loading.value = true
let resLoginInfo = {
password: encryptText(form.password),
username: form.username
}
store.dispatch("login",resLoginInfo).then(res=>{
toast("登录成功")
router.push("/")
}).finally(()=>{
loading.value = false
})
})
}
标签:const,text,return,instance,vue3,加密,vuex,JSEncrypt,vite From: https://blog.csdn.net/One__buyaomanggu/article/details/136841037