puzhibing
2023-02-15 2811bab657aab4145b65a45a824fb63e93b58e30
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.stylefeng.guns.modular.system.util.three;
 
public class Base64Coder {
    public final static char[] base64_alphabet = new char[] { '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 String encode(byte[] data) {
        int length = data.length;
        byte[] char_array_3 = new byte[] { 0, 0, 0 };
        byte[] char_array_4 = new byte[] { '=', '=', '=', '=' };
        String retContent = "";
        int i = 0;
        int j = 0;
        int reversePos = 0;
        while (length > 0) {
            length--;
            char_array_3[i++] = data[reversePos++];
            if (i == 3) {
                char_array_4[0] = (byte) ((char_array_3[0] & 0xfc) >> 2); // convert
                                                                            // the
                                                                            // char
                char_array_4[1] = (byte) (((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4));
                char_array_4[2] = (byte) (((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6));
                char_array_4[3] = (byte) (char_array_3[2] & 0x3f);
                for (i = 0; (i < 4); i++)
                    retContent += base64_alphabet[char_array_4[i]];
                i = 0;
            }
        }
 
        // handling the last input content
        if (i > 0) {
            for (j = i; j < 3; j++)
                char_array_3[j] = 0; // padding of zero
 
            char_array_4[0] = (byte) ((char_array_3[0] & 0xfc) >> 2); // right
                                                                        // shift
            char_array_4[1] = (byte) (((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4));
            char_array_4[2] = (byte) (((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6));
            char_array_4[3] = (byte) (char_array_3[2] & 0x3f);
 
            for (j = 0; (j < i + 1); j++)
                retContent += base64_alphabet[char_array_4[j]];
 
            while ((i++ < 3))
                // padding of '=' of output string
                retContent += '=';
 
        }
        return retContent;
    }
 
    public static String decode(String enContent) {
        byte[] data = enContent.getBytes();
        int i = 0, j = 0, enCode = 0;
        int mLength = data.length;
        byte[] char_array_4 = new byte[4];
        byte[] char_array_3 = new byte[3];
        String retContent = "";
 
        // filter out the padding '=' chars
        while (mLength > 0 && (((char) data[enCode]) != '=')
                && isBase64((char) data[enCode])) {
            mLength--;
            char_array_4[i++] = data[enCode++];
            if (i == 4) {
                for (i = 0; i < 4; i++)
                    char_array_4[i] = findChar((char) char_array_4[i]);
 
                char_array_3[0] = (byte) ((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
                char_array_3[1] = (byte) (((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
                char_array_3[2] = (byte) (((char_array_4[2] & 0x3) << 6) + char_array_4[3]);
 
                for (i = 0; (i < 3); i++)
                    retContent += (char) char_array_3[i];
                i = 0;
            }
        }
 
        // last content handling
        if (i > 0) {
            for (j = i; j < 4; j++)
                char_array_4[j] = 0;
 
            for (j = 0; j < 4; j++)
                char_array_4[j] = findChar((char) char_array_4[j]);
 
            char_array_3[0] = (byte) ((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
            char_array_3[1] = (byte) (((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
            char_array_3[2] = (byte) (((char_array_4[2] & 0x3) << 6) + char_array_4[3]);
 
            for (j = 0; (j < i - 1); j++)
                retContent += (char) char_array_3[j];
        }
 
        return retContent;
    }
 
    public static boolean isBase64(char c) {
        boolean base64 = false;
        for (int i = 0; i < 64; i++) {
            if (c == base64_alphabet[i]) {
                base64 = true;
                break;
            }
        }
        return base64;
    }
 
    public static byte findChar(char x) {
        byte index = 64; // 65th char '='
        for (int i = 0; i < 64; i++) {
            if (x == base64_alphabet[i]) {
                index = (byte) i;
                break;
            }
        }
        return index;
    }
 
}