java Base64
使用:
1、加密
1 final byte[] encoded; 2 ... 3 String encodeString = Base64.encode(encoded);
2、解密:
1 String secretMat; 2 ... 3 byte[] key = Base64.decode(secretMat);
3、Base64类:
1 public class Base64 { 2 3 static private final int BASELENGTH = 128; 4 static private final int LOOKUPLENGTH = 64; 5 static private final int TWENTYFOURBITGROUP = 24; 6 static private final int EIGHTBIT = 8; 7 static private final int SIXTEENBIT = 16; 8 static private final int FOURBYTE = 4; 9 static private final int SIGN = -128; 10 static private final char PAD = '='; 11 static private final boolean fDebug = false; 12 static final private byte[] base64Alphabet = new byte[BASELENGTH]; 13 static final private char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH]; 14 15 static { 16 for (int i = 0; i < BASELENGTH; ++i) { 17 base64Alphabet[i] = -1; 18 } 19 for (int i = 'Z'; i >= 'A'; i--) { 20 base64Alphabet[i] = (byte) (i - 'A'); 21 } 22 for (int i = 'z'; i >= 'a'; i--) { 23 base64Alphabet[i] = (byte) (i - 'a' + 26); 24 } 25 26 for (int i = '9'; i >= '0'; i--) { 27 base64Alphabet[i] = (byte) (i - '0' + 52); 28 } 29 30 base64Alphabet['+'] = 62; 31 base64Alphabet['/'] = 63; 32 33 for (int i = 0; i <= 25; i++) { 34 lookUpBase64Alphabet[i] = (char) ('A' + i); 35 } 36 37 for (int i = 26, j = 0; i <= 51; i++, j++) { 38 lookUpBase64Alphabet[i] = (char) ('a' + j); 39 } 40 41 for (int i = 52, j = 0; i <= 61; i++, j++) { 42 lookUpBase64Alphabet[i] = (char) ('0' + j); 43 } 44 lookUpBase64Alphabet[62] = (char) '+'; 45 lookUpBase64Alphabet[63] = (char) '/'; 46 47 } 48 49 private static boolean isWhiteSpace(char octect) { 50 return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9); 51 } 52 53 private static boolean isPad(char octect) { 54 return (octect == PAD); 55 } 56 57 private static boolean isData(char octect) { 58 return (octect < BASELENGTH && base64Alphabet[octect] != -1); 59 } 60 61 /** 62 * Encodes hex octects into Base64 63 * 64 * @param binaryData Array containing binaryData 65 * @return Encoded Base64 array 66 */ 67 public static String encode(byte[] binaryData) { 68 69 if (binaryData == null) { 70 return null; 71 } 72 73 int lengthDataBits = binaryData.length * EIGHTBIT; 74 if (lengthDataBits == 0) { 75 return ""; 76 } 77 78 int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP; 79 int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP; 80 int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1 81 : numberTriplets; 82 char encodedData[] = null; 83 84 encodedData = new char[numberQuartet * 4]; 85 86 byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0; 87 88 int encodedIndex = 0; 89 int dataIndex = 0; 90 if (fDebug) { 91 System.out.println("number of triplets = " + numberTriplets); 92 } 93 94 for (int i = 0; i < numberTriplets; i++) { 95 b1 = binaryData[dataIndex++]; 96 b2 = binaryData[dataIndex++]; 97 b3 = binaryData[dataIndex++]; 98 99 if (fDebug) { 100 System.out.println("b1= " + b1 + ", b2= " + b2 + ", b3= " + b3); 101 } 102 103 l = (byte) (b2 & 0x0f); 104 k = (byte) (b1 & 0x03); 105 106 byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) 107 : (byte) ((b1) >> 2 ^ 0xc0); 108 byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) 109 : (byte) ((b2) >> 4 ^ 0xf0); 110 byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) 111 : (byte) ((b3) >> 6 ^ 0xfc); 112 113 if (fDebug) { 114 System.out.println("val2 = " + val2); 115 System.out.println("k4 = " + (k << 4)); 116 System.out.println("vak = " + (val2 | (k << 4))); 117 } 118 119 encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; 120 encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)]; 121 encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3]; 122 encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f]; 123 } 124 125 // form integral number of 6-bit groups 126 if (fewerThan24bits == EIGHTBIT) { 127 b1 = binaryData[dataIndex]; 128 k = (byte) (b1 & 0x03); 129 if (fDebug) { 130 System.out.println("b1=" + b1); 131 System.out.println("b1<<2 = " + (b1 >> 2)); 132 } 133 byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) 134 : (byte) ((b1) >> 2 ^ 0xc0); 135 encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; 136 encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4]; 137 encodedData[encodedIndex++] = PAD; 138 encodedData[encodedIndex++] = PAD; 139 } else if (fewerThan24bits == SIXTEENBIT) { 140 b1 = binaryData[dataIndex]; 141 b2 = binaryData[dataIndex + 1]; 142 l = (byte) (b2 & 0x0f); 143 k = (byte) (b1 & 0x03); 144 145 byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) 146 : (byte) ((b1) >> 2 ^ 0xc0); 147 byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) 148 : (byte) ((b2) >> 4 ^ 0xf0); 149 150 encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; 151 encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)]; 152 encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2]; 153 encodedData[encodedIndex++] = PAD; 154 } 155 156 return new String(encodedData); 157 } 158 159 /** 160 * Decodes Base64 data into octects 161 * 162 * @param encoded string containing Base64 data 163 * @return Array containind decoded data. 164 */ 165 public static byte[] decode(String encoded) { 166 167 if (encoded == null) { 168 return null; 169 } 170 171 char[] base64Data = encoded.toCharArray(); 172 // remove white spaces 173 int len = removeWhiteSpace(base64Data); 174 175 if (len % FOURBYTE != 0) { 176 return null;// should be divisible by four 177 } 178 179 int numberQuadruple = (len / FOURBYTE); 180 181 if (numberQuadruple == 0) { 182 return new byte[0]; 183 } 184 185 byte decodedData[] = null; 186 byte b1 = 0, b2 = 0, b3 = 0, b4 = 0; 187 char d1 = 0, d2 = 0, d3 = 0, d4 = 0; 188 189 int i = 0; 190 int encodedIndex = 0; 191 int dataIndex = 0; 192 decodedData = new byte[(numberQuadruple) * 3]; 193 194 for (; i < numberQuadruple - 1; i++) { 195 196 if (!isData((d1 = base64Data[dataIndex++])) 197 || !isData((d2 = base64Data[dataIndex++])) 198 || !isData((d3 = base64Data[dataIndex++])) 199 || !isData((d4 = base64Data[dataIndex++]))) { 200 return null; 201 }// if found "no data" just return null 202 203 b1 = base64Alphabet[d1]; 204 b2 = base64Alphabet[d2]; 205 b3 = base64Alphabet[d3]; 206 b4 = base64Alphabet[d4]; 207 208 decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); 209 decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); 210 decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); 211 } 212 213 if (!isData((d1 = base64Data[dataIndex++])) 214 || !isData((d2 = base64Data[dataIndex++]))) { 215 return null;// if found "no data" just return null 216 } 217 218 b1 = base64Alphabet[d1]; 219 b2 = base64Alphabet[d2]; 220 221 d3 = base64Data[dataIndex++]; 222 d4 = base64Data[dataIndex++]; 223 if (!isData((d3)) || !isData((d4))) {// Check if they are PAD characters 224 if (isPad(d3) && isPad(d4)) { 225 if ((b2 & 0xf) != 0)// last 4 bits should be zero 226 { 227 return null; 228 } 229 byte[] tmp = new byte[i * 3 + 1]; 230 System.arraycopy(decodedData, 0, tmp, 0, i * 3); 231 tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4); 232 return tmp; 233 } else if (!isPad(d3) && isPad(d4)) { 234 b3 = base64Alphabet[d3]; 235 if ((b3 & 0x3) != 0)// last 2 bits should be zero 236 { 237 return null; 238 } 239 byte[] tmp = new byte[i * 3 + 2]; 240 System.arraycopy(decodedData, 0, tmp, 0, i * 3); 241 tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); 242 tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); 243 return tmp; 244 } else { 245 return null; 246 } 247 } else { // No PAD e.g 3cQl 248 b3 = base64Alphabet[d3]; 249 b4 = base64Alphabet[d4]; 250 decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); 251 decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); 252 decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); 253 254 } 255 256 return decodedData; 257 } 258 259 /** 260 * remove WhiteSpace from MIME containing encoded Base64 data. 261 * 262 * @param data the byte array of base64 data (with WS) 263 * @return the new length 264 */ 265 private static int removeWhiteSpace(char[] data) { 266 if (data == null) { 267 return 0; 268 } 269 270 // count characters that's not whitespace 271 int newSize = 0; 272 int len = data.length; 273 for (int i = 0; i < len; i++) { 274 if (!isWhiteSpace(data[i])) { 275 data[newSize++] = data[i]; 276 } 277 } 278 return newSize; 279 } 280 }

浙公网安备 33010602011771号