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

2021-03-04 07:26

阅读:318

标签: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 ) {
            codes[i] = -1;
        }
        for (int i = ‘A‘; i ) {
            codes[i] = (byte) (i - ‘A‘);
        }

        for (int i = ‘a‘; i ) {
            codes[i] = (byte) (26 + i - ‘a‘);
        }
        for (int i = ‘0‘; 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 ) {
            boolean quad = false;
            boolean trip = false;
            int val = (0xFF & (int) data[i]);
            val ;
            if ((i + 1)  data.length) {
                val |= (0xFF & (int) data[i + 1]);
                trip = true;
            }
            val ;
            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 ) {
            if ((data[ix] > 255) || codes[data[ix]] ) {
                --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 ) {
            int value = (data[ix] > 255) ? -1 : codes[data[ix]];
            if (value >= 0) {
                accum ;
                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


评论


亲,登录后才可以留言!