前端数据加密与解密

base64加密

install

cnpm install js-base64 –save-dev

封装
import { Base64 } from 'js-base64'

// 加盐
let salting = 'admin-authority'

// 加密
const encryptString = (name) => {
  return Base64.encode(name + salting)
}

// 解密
const decodeString = (name) => {
  let decodeName = Base64.decode(name) || ''

  if (decodeName && decodeName.split && decodeName.split(salting) && decodeName.split(salting)[0]) {
    return decodeName.split(salting)[0]
  } else {
    return ''
  }
}

export { encryptString, decodeString }
挂载
import { encryptString, decodeString } from '@/utils/encrypt'

Vue.prototype.encryptString = encryptString
Vue.prototype.decodeString = decodeString
应用
let str = '我是待加密的数据'
let enc = this.encryptString(str)
console.log(enc)            // 5oiR5piv5b6F5Yqg5a+G55qE5pWw5o2uYWRtaW4tYXV0aG9yaXR5
let dec = this.decodeString(enc)
console.log(dec)            // 我是待加密的数据

你可能感兴趣的:(base64)