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

一、前端加密

  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);

 

posted @ 2021-02-02 09:02  我命倾尘  阅读(2170)  评论(0编辑  收藏  举报