vue使用JSEncrypt 加密解密

2023-03-05  本文已影响0人  吃肉肉不吃肉肉

生成公钥私钥
https://www.bejson.com/enc/rsa/

安装依赖
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
    },
}
上一篇 下一篇

猜你喜欢

热点阅读