java下的Base64編解碼示例(2)

public class Base64Util {

    private static final char[] BASE64_TABLE = { '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', '+', '/', '=' };

    /**
     * @param args
     */
    public static void main(String[] args) {

        String s = "1234";

        // 49 50 51 52
        // 00110001 00110010 00110011 00110100
        // 00001100 00010011 00001000 00110011 00001101 00000000

        String enc = encryption(s);

        System.out.println(enc);

        s = decrypt(enc);

        System.out.println(s);
    }

    public static String encryption(String content) {

        byte[] b = content.getBytes();

        int length = b.length % 3 == 0 ? b.length / 3 * 4
                : (b.length / 3 + 1) * 4;
        char[] c = new char[length];
        int i = 3;
        int j = 4;
        while (i <= b.length) {
            c[j - 4] = BASE64_TABLE[b[i - 3] >> 2];
            c[j - 3] = BASE64_TABLE[((b[i - 3] % 4) << 4) + (b[i - 2] >> 4)];
            c[j - 2] = BASE64_TABLE[((b[i - 2] % 16) << 2) + (b[i - 1] >> 6)];
            c[j - 1] = BASE64_TABLE[b[i - 1] % 64];
            i += 3;
            j += 4;
        }

        if (i > b.length) {
            int x = b.length + 3 - i;
            if (x == 1) {
                c[j - 4] = BASE64_TABLE[b[i - 3] >> 2];
                c[j - 3] = BASE64_TABLE[(b[i - 3] % 4) << 4];
                c[j - 2] = BASE64_TABLE[BASE64_TABLE.length - 1];
                c[j - 1] = BASE64_TABLE[BASE64_TABLE.length - 1];
            } else if (x == 2) {
                // 00110001 00110010
                c[j - 4] = BASE64_TABLE[b[i - 3] >> 2];
                c[j - 3] = BASE64_TABLE[((b[i - 3] % 4) << 4) + (b[i - 2] >> 4)];
                c[j - 2] = BASE64_TABLE[(b[i - 2] % 16) << 2];
                c[j - 1] = BASE64_TABLE[BASE64_TABLE.length - 1];
            }
        }

        return new String(c);
    }

    public static String decrypt(String content) {

        byte[] b = content.getBytes();

        int equal = 0;

        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < BASE64_TABLE.length; j++) {
                if (b[i] == BASE64_TABLE[j]) {
                    b[i] = (byte) j;
                    break;
                }
            }
            if (b[i] == BASE64_TABLE.length - 1) {
                equal++;
            }
        }

        char[] c = new char[b.length / 4 * 3 - equal];

        int i = 3;
        int j = 4;

        while (j <= b.length) {

            // 49 50 51 52
            // 00110001 00110010 00110011 00110100
            // 00001100 00010011 00001000 00110011 00001101 00000000

            c[i - 3] = (char) ((b[j - 4] << 2) + (b[j - 3] >> 4));

            if (b[j - 2] != BASE64_TABLE.length - 1) {

                int x = ((b[j - 3] << 4) % 256);
                int y = (b[j - 2] >> 2);
                c[i - 2] = (char) (x + y);
            }

            if (b[j - 1] != BASE64_TABLE.length - 1) {
                c[i - 1] = (char) (((b[j - 2] << 6) % 256) + (b[j - 1]));
            }

            i += 3;
            j += 4;
        }

        return new String(c);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章