項目需求中需要對用戶登錄時的密碼進行加密,在網上查詢些許文章後,最終與後端協商使用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...