项目需求中须要对用户登陆时的密码进行加密,在网上查询些许文章后,最终与后端协商使用jsencrypt.js。前端
jsencrypt.js的github地址:https://github.com/travist/js...git
使用yarn安装至Vue项目github
yarn add jsencrypt --dep
或者使用npmnpm
npm install jsencrypt --dep
引入jsencrypt后端
import { JSEncrypt } from 'jsencrypt'
可封装为全局混合,便于调用
公钥为后端提供,如前端须要解密数据,则也须要后端提供私钥。函数
methods: { // 加密 encryptedData(publicKey, data) { // 新建JSEncrypt对象 let encryptor = new JSEncrypt(); // 设置公钥 encryptor.setPublicKey(publicKey); // 加密数据 return encryptor.encrypt(data); }, // 解密 decryptData(privateKey,data){ // 新建JSEncrypt对象 let decrypt= new JSEncrypt(); // 设置私钥 decrypt.setPrivateKey(privateKey); // 解密数据 decrypt.decrypt(secretWord); } }
调用函数加密,此处的公钥是我从后端那获取的,而后加密密码,这仅使用加密。this
encryptedPassword = this.encryptedData(publicKey, password);
即完成加密。
更多使用可查阅官方文档http://travistidwell.com/jsen...加密