项目需求中需要对用户登录时的密码进行加密,在网上查询些许文章后,最终与后端协商使用jsencrypt.js。
jsencrypt.js的github地址:https://github.com/travist/js...
使用yarn安装至Vue项目
yarn add jsencrypt --dep
或者使用npm
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);
}
}
调用函数加密,此处的公钥是我从后端那获取的,然后加密密码
encryptedPassword = this.encryptedData(publicKey, password);
即完成加密。
更多使用可查阅官方文档http://travistidwell.com/jsen...