安装依赖
npm install jsencrypt
import { JSEncrypt } from 'jsencrypt' // 引入
methods: {
// 公钥加密,私钥解密
passwordEncryption(param) { // 加密
const publicKey = '...' // 公钥
const encryptor = new JSEncrypt() // 新建JSEncrypt对象
encryptor.setPublicKey(publicKey) // 设置公钥
const passwordEncryp = encryptor.encrypt(param) // 对密码进行加密
return passwordEncryp
},
decrypt(msg) { // 解密
const privateKey = '...' // 私钥
const decrypt = new JSEncrypt()
decrypt.setPrivateKey(privateKey)
const decryptMsg = decrypt.decrypt(msg)
return decryptMsg
},
}