插件安装
npm install jsencrypt
在当前页面引入
import { JSEncrypt } from 'jsencrypt'
定义数据
data(){
return {
publicKey: ' ' // 此公钥可写死,也可调用接口获取
}
}
封装方法
methods: {
// 加密
encryptedData(publicKey, data) {
// 新建JSEncrypt对象
let encryptor = new JSEncrypt();
// 设置公钥
encryptor.setPublicKey(publicKey);
// 加密数据
return encryptor.encrypt(data);
}
}
调用
encryptedPassword = this.encryptedData(publicKey, password);