VUE前端AES, MD5, BASE64加密

以下加密方法安全全局方法

import cryptojs from 'crypto-js'

import {Base64 }from 'js-base64'


const AES = {

ecb: {

// AES-ECB 加密

    encrypt:function (content, key, iv) {

let skey =JSON.parse(sessionStorage.getItem('key'))

key = key ||skey.key

      iv = iv ||skey.iv

      if (!key || !iv) {

return

      }

let encrypted =cryptojs.AES.encrypt(

cryptojs.enc.Utf8.parse(content),

cryptojs.enc.Utf8.parse(key),

{

mode:cryptojs.mode.ECB,

padding:cryptojs.pad.Pkcs7

        }

)

return cryptojs.enc.Base64.stringify(encrypted.ciphertext)

},

// AES-ECB 解密

    decrypt:function (content, key, iv) {

let skey =JSON.parse(sessionStorage.getItem('key'))

key = key ||skey.key

      iv = iv ||skey.iv

      if (!key || !iv || !content) {

return

      }

let base64 =cryptojs.enc.Base64.parse(content)

let decrypt =cryptojs.AES.decrypt(

cryptojs.enc.Base64.stringify(base64),

cryptojs.enc.Utf8.parse(key),

{

mode:cryptojs.mode.ECB,

padding:cryptojs.pad.Pkcs7

        }

)

return cryptojs.enc.Utf8.stringify(decrypt).toString()

}

},

cbc: {

// AES-CBC 加密

    encrypt:function (content, key, iv) {

let skey =JSON.parse(sessionStorage.getItem('key'))

key = key ||skey.key

      iv = iv ||skey.iv

      switch (typeof (content)) {

case 'object':

content =JSON.stringify(content)

break

        default:

break

      }

let encrypted =cryptojs.AES.encrypt(

cryptojs.enc.Utf8.parse(content),

cryptojs.enc.Utf8.parse(key),

{

iv:cryptojs.enc.Utf8.parse(iv),

mode:cryptojs.mode.CBC,

padding:cryptojs.pad.Pkcs7

        }

)

return cryptojs.enc.Base64.stringify(encrypted.ciphertext)

},

// AES-CBC 解密

    decrypt:function (content, key, iv) {

let skey =JSON.parse(sessionStorage.getItem('key'))

key = key ||skey.key

      iv = iv ||skey.iv

      let base64 =cryptojs.enc.Base64.parse(content)

let decrypt =cryptojs.AES.decrypt(

cryptojs.enc.Base64.stringify(base64),

cryptojs.enc.Utf8.parse(key),

{

iv:cryptojs.enc.Utf8.parse(iv),

mode:cryptojs.mode.CBC,

padding:cryptojs.pad.Pkcs7

        }

)

return decrypt.toString(cryptojs.enc.Utf8).toString()

}

}

}

/**

* md5加密

* @param obj

*/

const md5 =function (obj) {

return (!!obj) &&cryptojs.MD5(obj).toString()

}

/**

* base64加密

* @param obj

* @returns {boolean|*}

*/

const base64 =function (obj) {

return (!!obj) &&Base64.encode(obj).toString()

}

// 拓展全局加解密方法

export default {

install (Vue) {

Vue.prototype.$aes =AES

    Vue.prototype.$md5 = md5

Vue.prototype.$base64 = base64

}

}

你可能感兴趣的:(VUE前端AES, MD5, BASE64加密)