Java Base64加密解密

package com.achonor.library;


import java.util.Arrays;

public class Base64 {
    private Base64(){}

    private static final char[] toBase64 = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
    };

    private  static final int[] fromBase64 = new int[256];
    static {
        Arrays.fill(fromBase64, -1);
        for (int i = 0; i < toBase64.length; i++)
            fromBase64[toBase64[i]] = i;
        fromBase64['='] = -2;
    }


    public static byte[] decode(byte[] srcByte){
        int rmd = 0;
        int slen = srcByte.length;
        while(0x3D == srcByte[slen - rmd - 1]){
            srcByte[slen - rmd - 1] = 0;
            rmd++;
        }
        int count = 0;
        int dlen = (srcByte.length / 4) * 3 - rmd;
        byte[] dest = new byte[dlen];
        for (int i = 0; i < srcByte.length; i += 4){
            int srcInt = (fromBase64[srcByte[i]] & 0x3f) << 18 |
                    (fromBase64[srcByte[i + 1]] & 0x3f) << 12 |
                    (fromBase64[srcByte[i + 2]] & 0x3f) << 6 |
                    (fromBase64[srcByte[i + 3]] & 0x3f);
            dest[count ++] = (byte)((srcInt >>> 16) & 0xff);
            if (count < dlen){
                dest[count ++] = (byte)((srcInt >>> 8) & 0xff);
            }
            if (count < dlen){
                dest[count ++] = (byte)((srcInt) & 0xff);
            }
        }
        return dest;
    }

    public static byte[] encode(byte[] srcByte) {
        int space = 0;
        int slen = srcByte.length;
        if (1 == srcByte.length % 3){
            space = 2;
            slen += 2;
        }else if (2 == srcByte.length % 3){
            space = 1;
            slen += 1;
        }
        int count = 0;
        int dlen = ((slen / 3) << 2);
        byte[] dest = new byte[dlen];
        for (int i = 0; i < slen; i += 3){
            byte srcByte1 = srcByte[i];
            byte srcByte2 = ((i + 1 < srcByte.length) ? srcByte[i + 1] : 0);
            byte srcByte3 = ((i + 2 < srcByte.length) ? srcByte[i + 2] : 0);

            int srcInt = (srcByte1 & 0xff) << 16|
                    (srcByte2 & 0xff) << 8|
                    (srcByte3 & 0xff);
            dest[count++] = (byte)toBase64[(srcInt >>> 18) & 0x3f];
            dest[count++] = (byte)toBase64[(srcInt >>> 12) & 0x3f];
            dest[count++] = (byte)toBase64[(srcInt >>> 6) & 0x3f];
            dest[count++] = (byte)toBase64[(srcInt) & 0x3f];
        }
        while (0 < space){
            space--;
            dest[--count] = 0x3D;
        }
        return dest;
    }




    public static byte[] decodeBase64(String srcStr){
        return decode(srcStr.getBytes());
    }

    public static String encodeBase64(byte[] srcByte){
        return new String(encode(srcByte));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

achonor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值