1 2 24 25 26 27 28 package com.lutris.util; 29 import java.io.ByteArrayOutputStream ; 30 import java.io.DataOutputStream ; 31 import java.io.IOException ; 32 import java.text.CharacterIterator ; 33 import java.text.StringCharacterIterator ; 34 35 43 public class Base64Encoder { 44 45 49 private static final char[] Base64Chars = { 50 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 51 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 52 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 53 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 54 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 55 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 56 'w', 'x', 'y', 'z', '0', '1', '2', '3', 57 '4', '5', '6', '7', '8', '9', '+', '/', 58 '=' 59 }; 60 61 66 private static final 67 char[] SessionKeyChars = 68 { 69 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 70 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 71 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 72 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 73 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 74 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 75 'w', 'x', 'y', 'z', '0', '1', '2', '3', 76 '4', '5', '6', '7', '8', '9', '_', '-', 77 '.' 78 }; 79 80 94 public static final String toBase64String(byte[] bytes) { 95 return toBase64String(bytes, Base64Chars); 96 } 97 98 108 public static final String toBase64SessionKeyString(byte[] bytes) { 109 return toBase64String(bytes, SessionKeyChars); 110 } 111 112 129 private static final String toBase64String(byte[] bytes, char[] chars) { 130 StringBuffer sb = new StringBuffer (); 131 int len = bytes.length, i=0, ival; 132 while (len >= 3) { 133 ival = ((int)bytes[i++] + 256) & 0xff; 134 ival <<= 8; 135 ival += ((int)bytes[i++] + 256) & 0xff; 136 ival <<= 8; 137 ival += ((int)bytes[i++] + 256) & 0xff; 138 len -= 3; 139 sb.append(chars[(ival >> 18) & 63]); 140 sb.append(chars[(ival >> 12) & 63]); 141 sb.append(chars[(ival >> 6) & 63]); 142 sb.append(chars[ival & 63]); 143 } 144 switch (len) { 145 case 0: break; 147 case 1: ival = ((int)bytes[i++] + 256) & 0xff; 149 ival <<= 16; 150 sb.append(chars[(ival >> 18) & 63]); 151 sb.append(chars[(ival >> 12) & 63]); 152 sb.append(chars[64]); 153 sb.append(chars[64]); 154 break; 155 case 2: ival = ((int)bytes[i++] + 256) & 0xff; 157 ival <<= 8; 158 ival += ((int)bytes[i] + 256) & 0xff; 159 ival <<= 8; 160 sb.append(chars[(ival >> 18) & 63]); 161 sb.append(chars[(ival >> 12) & 63]); 162 sb.append(chars[(ival >> 6) & 63]); 163 sb.append(chars[64]); 164 break; 165 } 166 return new String (sb); 167 } 168 169 177 public static final byte[] fromBase64String(String s) { 178 try { 179 StringCharacterIterator iter = new StringCharacterIterator (s); 180 ByteArrayOutputStream bytestr = new ByteArrayOutputStream (); 181 DataOutputStream outstr = new DataOutputStream (bytestr); 182 char c; 183 int d, i, group; 184 int[] bgroup = new int[4]; 185 decode: for (i=0, group=0, c = iter.first(); 186 c != CharacterIterator.DONE; 187 c = iter.next()) 188 { 189 switch (c) { 190 case 'A': d = 0; break; case 'B': d = 1; break; 191 case 'C': d = 2; break; case 'D': d = 3; break; 192 case 'E': d = 4; break; case 'F': d = 5; break; 193 case 'G': d = 6; break; case 'H': d = 7; break; 194 case 'I': d = 8; break; case 'J': d = 9; break; 195 case 'K': d = 10; break; case 'L': d = 11; break; 196 case 'M': d = 12; break; case 'N': d = 13; break; 197 case 'O': d = 14; break; case 'P': d = 15; break; 198 case 'Q': d = 16; break; case 'R': d = 17; break; 199 case 'S': d = 18; break; case 'T': d = 19; break; 200 case 'U': d = 20; break; case 'V': d = 21; break; 201 case 'W': d = 22; break; case 'X': d = 23; break; 202 case 'Y': d = 24; break; case 'Z': d = 25; break; 203 case 'a': d = 26; break; case 'b': d = 27; break; 204 case 'c': d = 28; break; case 'd': d = 29; break; 205 case 'e': d = 30; break; case 'f': d = 31; break; 206 case 'g': d = 32; break; case 'h': d = 33; break; 207 case 'i': d = 34; break; case 'j': d = 35; break; 208 case 'k': d = 36; break; case 'l': d = 37; break; 209 case 'm': d = 38; break; case 'n': d = 39; break; 210 case 'o': d = 40; break; case 'p': d = 41; break; 211 case 'q': d = 42; break; case 'r': d = 43; break; 212 case 's': d = 44; break; case 't': d = 45; break; 213 case 'u': d = 46; break; case 'v': d = 47; break; 214 case 'w': d = 48; break; case 'x': d = 49; break; 215 case 'y': d = 50; break; case 'z': d = 51; break; 216 case '0': d = 52; break; case '1': d = 53; break; 217 case '2': d = 54; break; case '3': d = 55; break; 218 case '4': d = 56; break; case '5': d = 57; break; 219 case '6': d = 58; break; case '7': d = 59; break; 220 case '8': d = 60; break; case '9': d = 61; break; 221 case '+': d = 62; break; case '/': d = 63; break; 222 case '_': d = 62; break; case '-': d = 63; break; 223 default: 224 break decode; } 228 bgroup[i++] = d; 229 if (i >= 4) { 230 i = 0; 231 group = ((bgroup[0] & 63) << 18)+((bgroup[1] & 63) << 12)+ 232 ((bgroup[2] & 63) << 6) + (bgroup[3] & 63); 233 outstr.writeByte(((group >> 16) & 255)); 234 outstr.writeByte(((group >> 8) & 255)); 235 outstr.writeByte(group & 255); 236 } 237 } 238 switch (i) { 242 case 2: 243 group = ((bgroup[0] & 63) << 18)+((bgroup[1] & 63) << 12); 245 outstr.writeByte(((group >> 16) & 255)); 246 break; 247 case 3: 248 group = ((bgroup[0] & 63) << 18)+((bgroup[1] & 63) << 12)+ 250 ((bgroup[2] & 63) << 6); 251 outstr.writeByte(((group >> 16) & 255)); 252 outstr.writeByte(((group >> 8) & 255)); 253 break; 254 default: 255 break; 258 } 259 outstr.flush(); 260 return bytestr.toByteArray(); 261 } 262 catch (IOException e) {} return null; 264 } 265 } 266 267 | Popular Tags |