Vue使用DES模式加密解密,包括Java加密解密

前端加密解密

1.安装:npm install crypto-js
2.创建一个crypto.ts文件

import CryptoJS from 'crypto-js'

export default {
    //加密
    encrypt(data){
        const key  = CryptoJS.enc.Utf8.parse('o08YQii9QF5MuzYj');
        let res = CryptoJS.DES.encrypt(JSON.stringify(data), key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        }).ciphertext.toString();
        return res;
    },
    //解密
    decrypt(data){
        const key  = CryptoJS.enc.Utf8.parse('o08YQii9QF5MuzYj');
        let res = CryptoJS.DES.decrypt({
            ciphertext: CryptoJS.enc.Hex.parse(data)
        }, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        return CryptoJS.enc.Utf8.stringify(res);
    }

}

3.在interceptors(拦截器)中进行加密解密

import Crypto from './crypto'

//request拦截器
http.interceptors.request.use((config) => {
  if(config.method === 'post') {
    config.data = Crypto.encrypt(config.data)
  }
  return config
}, (error) => {
  return Promise.reject(error)
})

//response拦截器
http.interceptors.response.use((respoens) => {
  respoens.data= JSON.parse(Crypto.decrypt(respoens.data));
  if(respoens.data.errorCode === -1 || !respoens.data.success) {
    Message.error(respoens.data.msg)
  }
  return respoens.data
}, (error) => {
  if (error && error.response) {
    switch (error.response.status) {
      case 500:
        Message.error('服务器繁忙')
        break
    }
  } else {
    Message.error('连接超时,请刷新页面')
  }
})

Java加密解密

参考:https://blog.csdn.net/u013000304/article/details/85772542

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值