码迷,mamicode.com
首页 > 编程语言 > 详细

Base64工具类:将前端vue与后台SpringBoot传输的参数进行加密和解密

时间:2021-02-02 11:36:40      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:dex   author   pac   int   auth   lazy   for   nbsp   turn   

一、前端加密

  1.引入base64依赖:

cnpm install --save js-base64

  技术图片

  2.使用base4对参数进行加密:

let Base64 = require(js-base64).Base64
//加密方法
let password =  Base64.encode(password);//解密方法
//let password = Base64.decode(password);

二、后台解密

  1.添加Base64Util.java工具类:

package com.gb.util;
import org.apache.commons.lang3.StringUtils;

/**
 * @author guob
 * 加密工具类
 */
@SuppressWarnings({ "unused"})
public class Base64Util {

    private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();
    private static byte[] codes = new byte[256];
    static {
        for (int i = 0; i < 256; i++) {
            codes[i] = -1;
        }
        for (int i = ‘A‘; i <= ‘Z‘; i++) {
            codes[i] = (byte) (i - ‘A‘);
        }

        for (int i = ‘a‘; i <= ‘z‘; i++) {
            codes[i] = (byte) (26 + i - ‘a‘);
        }
        for (int i = ‘0‘; i <= ‘9‘; i++) {
            codes[i] = (byte) (52 + i - ‘0‘);
        }
        codes[‘+‘] = 62;
        codes[‘/‘] = 63;
    }

    public Base64Util() {

    }

    /** 加密 */
    public static String encode(String data) {
        if (StringUtils.isNotBlank(data)) {
            //返回加密后的字符串
            return new String(encode(data.getBytes()));
        } else {
            return "字符串为空,无法进行加密";
        }
    }

    /** 解密 */
    public static String decode(String data) {
        if (StringUtils.isNotBlank(data)) {
            //返回解密后的字符串
            return new String(decode(data.toCharArray()));
        } else {
            return "字符串为空,无法进行解密";
        }
    }

    /** 编码 */
    public static char[] encode(byte[] data) {
        char[] out = new char[((data.length + 2) / 3) * 4];
        for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
            boolean quad = false;
            boolean trip = false;
            int val = (0xFF & (int) data[i]);
            val <<= 8;
            if ((i + 1) < data.length) {
                val |= (0xFF & (int) data[i + 1]);
                trip = true;
            }
            val <<= 8;
            if ((i + 2) < data.length) {
                val |= (0xFF & (int) data[i + 2]);
                quad = true;
            }
            out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
            val >>= 6;
            out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
            val >>= 6;
            out[index + 1] = alphabet[val & 0x3F];
            val >>= 6;
            out[index + 0] = alphabet[val & 0x3F];
        }
        return out;
    }

    /** 解码 */
    public static byte[] decode(char[] data) {
        int tempLen = data.length;
        for (int ix = 0; ix < data.length; ix++) {
            if ((data[ix] > 255) || codes[data[ix]] < 0) {
                --tempLen;
            }
        }
        int len = (tempLen / 4) * 3;
        if ((tempLen % 4) == 3) {
            len += 2;
        }
        if ((tempLen % 4) == 2) {
            len += 1;
        }
        byte[] out = new byte[len];
        int shift = 0;
        int accum = 0;
        int index = 0;
        for (int ix = 0; ix < data.length; ix++) {
            int value = (data[ix] > 255) ? -1 : codes[data[ix]];
            if (value >= 0) {
                accum <<= 6;
                shift += 6;
                accum |= value;
                if (shift >= 8) {
                    shift -= 8;
                    out[index++] = (byte) ((accum >> shift) & 0xff);
                }
            }
        }
        if (index != out.length) {
            throw new Error("错误计算的数据长度(写入" + index + "而不是" + out.length + ")");
        }
        return out;
    }
}

  2.使用工具类对接收到参数进行解密:

String password = Base64Util.decode(password);

 

Base64工具类:将前端vue与后台SpringBoot传输的参数进行加密和解密

标签:dex   author   pac   int   auth   lazy   for   nbsp   turn   

原文地址:https://www.cnblogs.com/guobin-/p/14360240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!