1.安装

npm install jsencrypt --dev 

2,建立jsencrypt.js文件,内容如下
通过公钥加密,私钥解密,哪个页面需要引入utils/jsencrypt文件即可

import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'

// 密钥对生成 http://web.chacuo.net/netrsakeypair; 把下面生成的公钥、私钥换成自己生成的即可
const publicKey = '',//生成的公钥
const privateKey='',

export function encrypt(txt) {
    const encryptor = new JSEncrypt()
    encryptor.setPublicKey(publicKey) // 设置公钥
    const en = encodeURIComponent(encryptor.encrypt(txt)) // 对数据进行加密
    return en
    // encodeURIComponent()   js原生函数  加密前将所有的"中文/" encoder掉
    // 后端在转回来 java.net.URLDecoder.decode(en,"UTF-8")
}

// 解密
export function decrypt(txt) {
  const encryptor = new JSEncrypt()
  encryptor.setPrivateKey(privateKey) // 设置私钥
  return encryptor.decrypt(txt) // 对数据进行解密
}

3.页面使用

import { encrypt, decrypt } from '@/utils/jsencrypt'//rememberMe-password加密

Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });//存到cookies时加密

this.loginForm.password : decrypt(password),//取出时解密

参考链接:https://blog.csdn.net/weixin_46172085/article/details/122255594
参考链接:https://blog.csdn.net/qq_25623257/article/details/109775531?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1-109775531-blog-122886177.pc_relevant_vip_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1-109775531-blog-122886177.pc_relevant_vip_default&utm_relevant_index=1

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐