base64 java实现

理论知识主要参考:Base64笔记 - 阮一峰的网络日志 (ruanyifeng.com),欢迎读者关注原作者

概念:所谓Base64,就是说选出64个字符----小写字母a-z、大写字母A-Z、数字0-9、符号"+"、"/"(再加上作为垫字的"=",实际上是65个字符)----作为一个基本字符集。然后,其他所有符号都转换成这个字符集中的字符。

java代码实现:

package com.example.security.security;

import java.io.UnsupportedEncodingException;

public class Base64Util {

    private static String base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    private static int[] base64DecodeChars = new int[]{
            -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, -1, -1, -1, -1, -1};


    public static String base64encode(String str, String charsetName) throws UnsupportedEncodingException {
        byte[] bytes = str.getBytes(charsetName);
        int length = bytes.length;
        int i = 0;
        StringBuilder stringBuilder = new StringBuilder();  // 保存结果
        byte a, b, c;
        while(i < length){
            a = bytes[i++];
            if(i == length){
                // 只有一个元素的情况,还剩一个元素情况
                int t1 = (a & 0xFC) >> 2;  // 高 6 位, 第一步取出高6位,然后去除两个0
                int t2 = (a & 0x3) << 4;  // 低两位,增加4个0
                stringBuilder.append(base64EncodeChars.charAt(t1));
                stringBuilder.append(base64EncodeChars.charAt(t2));
                stringBuilder.append('=');
                stringBuilder.append('=');
                break;
            }
            b = bytes[i++];
            if(i == length){
                // 只要两个元素的情况
                int t1 = (a & 0xFC) >> 2;  // 高 6 位, 第一步取出高6位,然后去除两个0
                int t2 = ((a & 0x3) << 4) + ((b & 0xF0) >> 4) ;  // 取出a的低2位,然后增加4个0,然后加上b的高4位
                int t3 = (b & 0xF) << 2;
                stringBuilder.append(base64EncodeChars.charAt(t1));
                stringBuilder.append(base64EncodeChars.charAt(t2));
                stringBuilder.append(base64EncodeChars.charAt(t3));
                stringBuilder.append('=');

                break;
            }
            c = bytes[i++];
            int t1 = (a & 0xFC) >> 2;  // 高 6 位, 第一步取出高6位,然后去除两个0
            int t2 = ((a & 0x3) << 4) + ((b & 0xF0) >> 4) ;  // 取出a的低2位,然后增加4个0,然后加上b的高4位
            int t3 = ((b & 0xF) << 2) + ((c & 0xC0) >> 6) ;          // 取出b的低4位,并且增加两个0,然后加上c的高2位(去除6个0);
            int t4 = c & 0x3F;
            stringBuilder.append(base64EncodeChars.charAt(t1));
            stringBuilder.append(base64EncodeChars.charAt(t2));
            stringBuilder.append(base64EncodeChars.charAt(t3));
            stringBuilder.append(base64EncodeChars.charAt(t4));
        }

        return stringBuilder.toString();

    }

    public static String base64decode(String str, String charset) throws UnsupportedEncodingException {
        byte[] bytes = new byte[(str.length()/4)*3];
        int j = 0;
        for(int i=0; i> 4));
            // b的低4位,然后加4个0,c的高6位(最高两位是0),并且去除两个0
            bytes[j++] = (byte) (((base64DecodeChars[b] & 0xF) << 4) + ((base64DecodeChars[c] & 0xFC) >> 2));

            bytes[j++] = (byte) (((base64DecodeChars[c]&0x03) << 6) + base64DecodeChars[d]);
        }

        while (j > 0 && bytes[--j] == -1 ){
            bytes[j] = 0;
        }
        return new String(bytes, charset);
    }


    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "123";
        String s = base64encode(str, "utf-8");
        System.out.println(s);

        String s1 = base64decode(s, "utf-8");
        System.out.println(s1);

    }
}

你可能感兴趣的:(base64 java实现)