Java实现Base64的编码解码

没用使用java自带的各种Base64的方法以及第三方库,纯手打,若有测试异常请及时告知!

java.math.BigInteger在字符串与二进制转换上是个好东西,详查API。

package Base64;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

public class Base64_Function {
    // 编码索引表
    public static char[] indexEncode = { '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', '+', '/' };
    // 解码索引表
    public static int[] indexDecode = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1,
            63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31,
            32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };

    public static String Encoding(String inputStr) throws UnsupportedEncodingException {
        String s = new BigInteger(1, inputStr.getBytes("utf-8")).toString(2);
        while (s.length() % 8 != 0) { // BigInteger.toString()的缺陷,首个字节没法八位补齐,只能手动处理,例如首位是00010110,输出的只有10110
            s = "0" + s;
        }
        while (s.length() % 6 != 0) { // 针对Base64每6位截取一段,补齐不足6位的最后一个字节
            s += "0";
        }
        String outputStr = "";
        for (int i = 0; i < s.length(); i += 6) {
            String newByte = s.substring(i, i + 6);
            int index = new BigInteger(newByte, 2).intValue();
            outputStr += indexEncode[index];
        }
        while (outputStr.length() % 4 != 0) { // 针对Base64每4个字节为一组,用"="补齐
            outputStr += "=";
        }
        return outputStr;
    }

    public static String Decoding(String inputStr) throws UnsupportedEncodingException {
        if (inputStr.length() % 4 != 0) { // 检查Base64编码每四个字节一组
            return "Base64编码格式错误,请重新检查!";
        }
        String s = "";
        for (int i = 0; i < inputStr.length(); i++) {
            int index = inputStr.charAt(i);
            if (index == '=') { // "="为补位,遇到了即代表结束
                break;
            }
            if (index >= indexDecode.length || indexDecode[index] == -1) { // 通过ASCII值检查Base64编码合法
                return "Base64编码格式错误,请重新检查!";
            }
            String tempStr = Integer.toBinaryString(indexDecode[inputStr.charAt(i)]);
            while (tempStr.length() != 6) { // 将每个字节还原成二进制,不足6位的前面补位(编码过程中截取6位为一个字节)
                tempStr = "0" + tempStr;
            }
            s += tempStr;
        }
        byte[] b = new byte[s.length() / 8];
        for (int i = 0, j = 0; i < b.length; i++, j += 8) { // 重新按8位截取一个字节,之前为补6位而补的0将直接被舍弃
            b[i] = new BigInteger(s.substring(j, j + 8), 2).byteValue();
        }
        String outputStr = new String(b, "utf-8");
        return outputStr;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        System.out.println(Encoding("你好啦啦啦啦"));
        System.out.println(Decoding("5L2g5aW95ZWm5ZWm5ZWm5ZWm"));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值