前端使用RSA加密
目录
前端使用RSA加密
安装第三方库
npm install jsencrypt --save
引入
// 全局引入在 main.js 文件中
import JSEncrypt from "jsencrypt";
Vue.prototype.jsEncrypt = JSEncrypt;
// 局部使用,在需要使用 RSA 加密的文件中
import JSEncrypt from 'jsencrypt'
使用
// 加密
const jsRsa = new JSEncrypt();
//设置公钥 公钥是由后端返回的
jsRsa.setPublicKey('公钥');
//使用加密对象给密码加密
this.user.password = jsRsa.encrypt(this.user.password);
// 解密
var decrypt = new JSEncrypt();
//设置私钥 私钥也是从后端拿的
decrypt.setPrivateKey('私钥');
// 解密
this.user.password = decrypt.decrypt(this.user.password);