vue前端RSA加密

1、安装jsencrypt

cnpm install jsencrypt --save

2、在main.js中引入jsencrypt

import JSEncrypt from 'jsencrypt'

const PUBLIC_KEY = '你的公钥字符串'

Vue.prototype.$getRsaCode = (str) => { // 注册方法

  let pubKey = `-----BEGIN PUBLIC KEY-----${PUBLIC_KEY}-----END PUBLIC KEY-----` // ES6 模板字符串 引用 rsa 公钥

  let encryptStr = new JSEncrypt()

  encryptStr.setPublicKey(pubKey) // 设置 加密公钥

  let data = encryptStr.encrypt(str.toString()) // 进行加密

  return data

}

3、调用登录方法时,调用getRsaCode 方法

login({

     name: _this.username,

     pwd: _this.$getRsaCode(_this.password)

}).then((res) => {

      // ...

})

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。