一、安装
npm install jsencrypt --save
二、在main.js中引用依赖
// 导入RSA加密
import JSEncrypt from 'jsencrypt';
Vue.prototype.$getRsaCode = function(str) {
// 注册方法
let pubKey = `M---引入公钥`; // ES6 模板字符串 引用 rsa 公钥
let encryptStr = new JSEncrypt();
encryptStr.setPublicKey(pubKey); // 设置 加密公钥
let data = encryptStr.encrypt(str.toString()); // 进行加密
return data;
};
三、在项目中使用RSA加密 (this.$getRsaCode(‘要加密的数据’))
加密结果:
后端获得加密的内容后,会通过后端的私钥进行解密