Vue 使用crypto-js实现登录信息加密

vue yekong

Vue 使用crypto-js实现登录信息加密

需求

vue项目开发时,需要使用crypto-js对账号密码进行加密处理。

安装依赖

npm install crypto-js --save-dev

创建方法

const CryptoJS = require('crypto-js');

export function encrypt(data) {
  var key = CryptoJS.enc.Latin1.parse('123');
  var iv = CryptoJS.enc.Latin1.parse('123');
  return CryptoJS.AES.encrypt(data, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding
  }).toString();
}

使用

login() {
  var that = this
  if (!that.userName) {
    that.$message({
      message: '请输入用户名',
      type: 'warning'
    });
    return;
  }
  if (!that.userPassword) {
    that.$message({
      message: '请输入密码',
      type: 'warning'
    });
    return;
  }
  if (!that.vrifyCode) {
    that.$message({
      message: '请输入验证码',
      type: 'warning'
    });
    return;
  }
  bsLogin({
    userName: encrypt(this.userName),
    userPassword: encrypt(this.userPassword),
    vrifyCode: this.vrifyCode
  }).then((res) => {
    if (res.status == 200) {
      that.$message({
        message: '登录成功',
        type: 'success'
      });
    }
  })
},
喜欢