nuxt.js中 前端 加密解密

在~/plugins下新建 encrytion.js文件

const crypto = require('crypto');

export default {
  aesEncrypt(data, key){
    try{
      var salt =  key.toString() + 'manbanzhen';    // 定义盐值
      const cipher = crypto.createCipher('aes192', salt);   // 采用aes192加密方式
      var crypted = cipher.update(data, 'utf8', 'hex');     // 加密
      crypted += cipher.final('hex');
      return crypted;
    }catch (e) {        // 加捕获是为了在验证失败的时候,直接让用户重新登陆
      alert("检测到存在安全异常,请检查当前网络环境并重新登录!");
      window.location.href = '/login'
    }

    },

  aesDecrypt(encrypted, key) {
    try{
      var salt =  key.toString() + 'manbanzhen';    // 定义盐值
      const decipher = crypto.createDecipher('aes192', salt);   // 解密 aes192
      var decrypted = decipher.update(encrypted, 'hex', 'utf8');    //解密
      decrypted += decipher.final('utf8');
      return decrypted;
    }catch (e) {        // 加捕获是为了在验证失败的时候,直接让用户重新登陆
      // console.log("error*-**--*-")
      alert("检测到存在安全异常,请检查当前网络环境并重新登录!");
      window.location.href = '/login';
    }
    }
}

在需要的地方进行引用:

import encrytion from '~/plugins/encrytion'

// 加密
encrytion.aesEncrypt('待加密的内容''密钥')
// 解密
encrytion.aesDecrypt('待解密的密文''密钥')

你可能感兴趣的:(nuxt.js,开发笔记)