Java Base64加密解密

public class HBase64 {
    // 编码使用的基础字符,存入字符数组中去。

    private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@/=".toCharArray();

    /**
     * Base64 encode the given data 进行编码。
     */
    public static String encode(byte[] data) {
        int start = 0;
        int len = data.length;
        StringBuffer buf = new StringBuffer(data.length * 3 / 2);

        int end = len - 3;
        int i = start;
        // /编码的实现过程。
        while (i <= end) {
            int d = ((((int) data[i]) & 0x0ff) << 16)
                    | ((((int) data[i + 1]) & 0x0ff) << 8)
                    | (((int) data[i + 2]) & 0x0ff);

            buf.append(legalChars[(d >> 18) & 63]);
            buf.append(legalChars[(d >> 12) & 63]);
            buf.append(legalChars[(d >> 6) & 63]);
            buf.append(legalChars[d & 63]);

            i += 3;
        }
        // 对于数据最后几位的处理。
        if (i == start + len - 2) {
            int d = ((((int) data[i]) & 0x0ff) << 16)
                    | ((((int) data[i + 1]) & 255) << 8);

            buf.append(legalChars[(d >> 18) & 63]);
            buf.append(legalChars[(d >> 12) & 63]);
            buf.append(legalChars[(d >> 6) & 63]);
            buf.append("=");
        } else if (i == start + len - 1) {
            int d = (((int) data[i]) & 0x0ff) << 16;

            buf.append(legalChars[(d >> 18) & 63]);
            buf.append(legalChars[(d >> 12) & 63]);
            buf.append("==");
        }
        // 返回转换完毕的数据。
        data = null;
        return buf.toString();
    }

    /**
     * 对接收的数据进行64解码
     * @param 待解码的字符串
     * @param os 将完成解码的输出流
     * @throws IOException
     */
    private static void decodeII(String s, OutputStream os) throws IOException {
        char[] chData = s.toCharArray();

        int i = 0;
        int len = s.length();
        while (true) {
            if (i == len - 1) {
                break;
            }
            int tri = ((getIndex(chData[i])) << 18)
                    | ((getIndex(chData[i + 1])) << 12)
                    | ((getIndex(chData[i + 2])) << 6)
                    | ((getIndex(chData[i + 3])));

            os.write(((tri >> 16) & 255));
            if (chData[i + 2] == '=') {
                break;
            }
            os.write(((tri >> 8) & 255));
            if (chData[i + 3] == '=') {
                break;
            }
            os.write((tri & 255));
            i += 4;
        }
    }

    /**
     * 返回完成解码的字符串
     * @param 待解码字符串
     * @return
     */
    public static String decode(String s) {
        ByteArrayOutputStream str = new ByteArrayOutputStream();
        try {
            decodeII(s, str);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String data = str.toString();
        return data;

    }

    /**
     * 返回字符c在法则字符中的位置值
     * @param 测试字符
     * @return 返回位置值
     */
    public static int getIndex(char c) {

        for (int i = 0; i < legalChars.length; i++) {
            if (legalChars[i] == c) {
                return i;
            }
        }

        return -1;
    }
}

调用如下:

byte[] bytes=new byte[];
HBase64.encode(bytes);
原文地址:https://www.cnblogs.com/wangning-aaron/p/3880781.html