首页

关于fastjson的Base64类实现对base64算法解密方法源码说明

标签:base64加解密算法,alibaba,fastjson     发布时间:2018-04-23   

一、前言

关于fastjson的com.alibaba.fastjson.util.Base64实现base64解密算法,分别对char[]、字符串进行解密为二进制数组,具体参见如下代码示例。

二、源码说明

package com.alibaba.fastjson.util;@b@@b@import java.util.Arrays;@b@@b@ @b@public class Base64 {@b@@b@    public static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();@b@    public static final int[]  IA = new int[256];@b@    static {@b@        Arrays.fill(IA, -1);@b@        for (int i = 0, iS = CA.length; i < iS; i++)@b@            IA[CA[i]] = i;@b@        IA['='] = 0;@b@    }@b@@b@    /**@b@     * Decodes a BASE64 encoded char array that is known to be resonably well formatted. The method is about twice as@b@     * fast as #decode(char[]). The preconditions are:<br>@b@     * + The array must have a line length of 76 chars OR no line separators at all (one line).<br>@b@     * + Line separator must be "\r\n", as specified in RFC 2045 + The array must not contain illegal characters within@b@     * the encoded string<br>@b@     * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>@b@     *@b@     * @param chars The source array. Length 0 will return an empty array. <code>null</code> will throw an exception.@b@     * @return The decoded array of bytes. May be of length 0.@b@     */@b@    public static byte[] decodeFast(char[] chars, int offset, int charsLen) {@b@        // Check special case@b@        if (charsLen == 0) {@b@            return new byte[0];@b@        }@b@@b@        int sIx = offset, eIx = offset + charsLen - 1; // Start and end index after trimming.@b@@b@        // Trim illegal chars from start@b@        while (sIx < eIx && IA[chars[sIx]] < 0)@b@            sIx++;@b@@b@        // Trim illegal chars from end@b@        while (eIx > 0 && IA[chars[eIx]] < 0)@b@            eIx--;@b@@b@        // get the padding count (=) (0, 1 or 2)@b@        int pad = chars[eIx] == '=' ? (chars[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end.@b@        int cCnt = eIx - sIx + 1; // Content count including possible separators@b@        int sepCnt = charsLen > 76 ? (chars[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;@b@@b@        int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes@b@        byte[] bytes = new byte[len]; // Preallocate byte[] of exact length@b@@b@        // Decode all but the last 0 - 2 bytes.@b@        int d = 0;@b@        for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {@b@            // Assemble three bytes into an int from four "valid" characters.@b@            int i = IA[chars[sIx++]] << 18 | IA[chars[sIx++]] << 12 | IA[chars[sIx++]] << 6 | IA[chars[sIx++]];@b@@b@            // Add the bytes@b@            bytes[d++] = (byte) (i >> 16);@b@            bytes[d++] = (byte) (i >> 8);@b@            bytes[d++] = (byte) i;@b@@b@            // If line separator, jump over it.@b@            if (sepCnt > 0 && ++cc == 19) {@b@                sIx += 2;@b@                cc = 0;@b@            }@b@        }@b@@b@        if (d < len) {@b@            // Decode last 1-3 bytes (incl '=') into 1-3 bytes@b@            int i = 0;@b@            for (int j = 0; sIx <= eIx - pad; j++)@b@                i |= IA[chars[sIx++]] << (18 - j * 6);@b@@b@            for (int r = 16; d < len; r -= 8)@b@                bytes[d++] = (byte) (i >> r);@b@        }@b@@b@        return bytes;@b@    }@b@@b@    public static byte[] decodeFast(String chars, int offset, int charsLen) {@b@        // Check special case@b@        if (charsLen == 0) {@b@            return new byte[0];@b@        }@b@@b@        int sIx = offset, eIx = offset + charsLen - 1; // Start and end index after trimming.@b@@b@        // Trim illegal chars from start@b@        while (sIx < eIx && IA[chars.charAt(sIx)] < 0)@b@            sIx++;@b@@b@        // Trim illegal chars from end@b@        while (eIx > 0 && IA[chars.charAt(eIx)] < 0)@b@            eIx--;@b@@b@        // get the padding count (=) (0, 1 or 2)@b@        int pad = chars.charAt(eIx) == '=' ? (chars.charAt(eIx - 1) == '=' ? 2 : 1) : 0; // Count '=' at end.@b@        int cCnt = eIx - sIx + 1; // Content count including possible separators@b@        int sepCnt = charsLen > 76 ? (chars.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0;@b@@b@        int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes@b@        byte[] bytes = new byte[len]; // Preallocate byte[] of exact length@b@@b@        // Decode all but the last 0 - 2 bytes.@b@        int d = 0;@b@        for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {@b@            // Assemble three bytes into an int from four "valid" characters.@b@            int i = IA[chars.charAt(sIx++)] << 18 | IA[chars.charAt(sIx++)] << 12 | IA[chars.charAt(sIx++)] << 6 | IA[chars.charAt(sIx++)];@b@@b@            // Add the bytes@b@            bytes[d++] = (byte) (i >> 16);@b@            bytes[d++] = (byte) (i >> 8);@b@            bytes[d++] = (byte) i;@b@@b@            // If line separator, jump over it.@b@            if (sepCnt > 0 && ++cc == 19) {@b@                sIx += 2;@b@                cc = 0;@b@            }@b@        }@b@@b@        if (d < len) {@b@            // Decode last 1-3 bytes (incl '=') into 1-3 bytes@b@            int i = 0;@b@            for (int j = 0; sIx <= eIx - pad; j++)@b@                i |= IA[chars.charAt(sIx++)] << (18 - j * 6);@b@@b@            for (int r = 16; d < len; r -= 8)@b@                bytes[d++] = (byte) (i >> r);@b@        }@b@@b@        return bytes;@b@    }@b@@b@    /**@b@     * Decodes a BASE64 encoded string that is known to be resonably well formatted. The method is about twice as fast@b@     * as decode(String). The preconditions are:<br>@b@     * + The array must have a line length of 76 chars OR no line separators at all (one line).<br>@b@     * + Line separator must be "\r\n", as specified in RFC 2045 + The array must not contain illegal characters within@b@     * the encoded string<br>@b@     * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>@b@     *@b@     * @param s The source string. Length 0 will return an empty array. <code>null</code> will throw an exception.@b@     * @return The decoded array of bytes. May be of length 0.@b@     */@b@    public static byte[] decodeFast(String s) {@b@        // Check special case@b@        int sLen = s.length();@b@        if (sLen == 0) {@b@            return new byte[0];@b@        }@b@@b@        int sIx = 0, eIx = sLen - 1; // Start and end index after trimming.@b@@b@        // Trim illegal chars from start@b@        while (sIx < eIx && IA[s.charAt(sIx) & 0xff] < 0)@b@            sIx++;@b@@b@        // Trim illegal chars from end@b@        while (eIx > 0 && IA[s.charAt(eIx) & 0xff] < 0)@b@            eIx--;@b@@b@        // get the padding count (=) (0, 1 or 2)@b@        int pad = s.charAt(eIx) == '=' ? (s.charAt(eIx - 1) == '=' ? 2 : 1) : 0; // Count '=' at end.@b@        int cCnt = eIx - sIx + 1; // Content count including possible separators@b@        int sepCnt = sLen > 76 ? (s.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0;@b@@b@        int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes@b@        byte[] dArr = new byte[len]; // Preallocate byte[] of exact length@b@@b@        // Decode all but the last 0 - 2 bytes.@b@        int d = 0;@b@        for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {@b@            // Assemble three bytes into an int from four "valid" characters.@b@            int i = IA[s.charAt(sIx++)] << 18 | IA[s.charAt(sIx++)] << 12 | IA[s.charAt(sIx++)] << 6@b@                    | IA[s.charAt(sIx++)];@b@@b@            // Add the bytes@b@            dArr[d++] = (byte) (i >> 16);@b@            dArr[d++] = (byte) (i >> 8);@b@            dArr[d++] = (byte) i;@b@@b@            // If line separator, jump over it.@b@            if (sepCnt > 0 && ++cc == 19) {@b@                sIx += 2;@b@                cc = 0;@b@            }@b@        }@b@@b@        if (d < len) {@b@            // Decode last 1-3 bytes (incl '=') into 1-3 bytes@b@            int i = 0;@b@            for (int j = 0; sIx <= eIx - pad; j++)@b@                i |= IA[s.charAt(sIx++)] << (18 - j * 6);@b@@b@            for (int r = 16; d < len; r -= 8)@b@                dArr[d++] = (byte) (i >> r);@b@        }@b@@b@        return dArr;@b@    }@b@}