第一步,下载
npm install jsencrypt --dev
第二步,建立文件,内容如下
通过公钥加密,私钥解密,哪个页面需要引入utils/jsencrypt文件即可
import JSEncrypt from 'jsencrypt/bin/'
// 密钥对生成 /netrsakeypair; 把下面生成的公钥、私钥换成自己生成的即可
const publicKey = '',//生成的公钥
const privateKey='',
// 加密
export function encrypt(txt) {
const encryptor = new JSEncrypt()
(publicKey) // 设置公钥
return (txt) // 对数据进行加密
}
// 解密
export function decrypt(txt) {
const encryptor = new JSEncrypt()
(privateKey) // 设置私钥
return (txt) // 对数据进行解密
}
第三步,页面使用
import { encrypt, decrypt } from '@/utils/jsencrypt'//rememberMe-password加密
("password", encrypt(), { expires: 30 });//存到cookies时加密
: decrypt(password),//取出时解密