1 19 package org.lucane.common.crypto; 20 21 import java.security.*; 22 23 24 28 public class Base64 29 { 30 36 public static String encode(byte[] raw) 37 { 38 int encLen = ((raw.length + 2) / 3) * 4; 39 StringBuffer encoded = new StringBuffer (encLen); 40 int len3 = (raw.length / 3) * 3; 41 42 for(int i = 0; i < len3; i += 3) 43 encoded.append(encodeFullBlock(raw, i)); 44 45 if(len3 < raw.length) 46 encoded.append(encodeBlock(raw, len3)); 47 48 return encoded.toString(); 49 } 50 51 57 public static byte[] decode(String base64) 58 { 59 try 60 { 61 int pad = 0; 62 63 for(int i = base64.length() - 1; 64 (i > 0) && (base64.charAt(i) == '='); 65 i--) 66 pad++; 67 68 int length = base64.length() / 4 * 3 - pad; 69 byte[] raw = new byte[length]; 70 71 for(int i = 0, rawIndex = 0; i < base64.length(); i += 4, rawIndex += 3) 72 { 73 int block = (getValue(base64.charAt(i)) << 18) + 74 (getValue(base64.charAt(i + 1)) << 12) + 75 (getValue(base64.charAt(i + 2)) << 6) + 76 (getValue(base64.charAt(i + 3))); 77 78 for(int j = 2; j >= 0; j--) 79 { 80 if(rawIndex + j < raw.length) 81 raw[rawIndex + j] = (byte)(block & 0xff); 82 83 block >>= 8; 84 } 85 } 86 87 return raw; 88 } 89 catch(Exception e) 90 { 91 return new byte[0]; 92 } 93 } 94 95 101 public static String encodeKey(Key key) 102 { 103 return encode(key.getEncoded()); 104 } 105 106 private static char[] encodeBlock(byte[] raw, int offset) 107 { 108 int block = 0; 109 int slack = raw.length - offset - 1; 110 int end = (slack >= 2) ? 2 : slack; 111 112 for(int i = 0; i < 3; i++) 113 { 114 115 byte b = (offset + i < raw.length) ? raw[offset + i] : 0; 116 int neuter = (b < 0) ? b + 256 : b; 117 block <<= 8; 118 block += neuter; 119 } 120 121 char[] base64 = new char[4]; 122 123 for(int i = 3; i >= 0; i--) 124 { 125 126 int sixBit = block & 0x3f; 127 base64[i] = getChar(sixBit); 128 block >>= 6; 129 } 130 131 if(slack < 1) 132 base64[2] = '='; 133 134 if(slack < 2) 135 base64[3] = '='; 136 137 return base64; 138 } 139 140 private static char[] encodeFullBlock(byte[] raw, int offset) 141 { 142 int block = 0; 143 144 for(int i = 0; i < 3; i++) 145 { 146 block <<= 8; 147 block += (0xff & raw[offset + i]); 148 } 149 150 block = ((raw[offset] & 0xff) << 16) + ((raw[offset + 1] & 0xff) << 8) + 151 (raw[offset + 2] & 0xff); 152 153 char[] base64 = new char[4]; 154 155 for(int i = 3; i >= 0; i--) 156 { 157 int sixBit = block & 0x3f; 158 base64[i] = getChar(sixBit); 159 block >>= 6; 160 } 161 162 return base64; 163 } 164 165 private static char getChar(int sixBit) 166 { 167 if((sixBit >= 0) && (sixBit < 26)) 168 return (char)('A' + sixBit); 169 170 if((sixBit >= 26) && (sixBit < 52)) 171 return (char)('a' + (sixBit - 26)); 172 173 if((sixBit >= 52) && (sixBit < 62)) 174 return (char)('0' + (sixBit - 52)); 175 176 if(sixBit == 62) 177 return '+'; 178 179 if(sixBit == 63) 180 return '/'; 181 182 return '?'; 183 } 184 185 private static int getValue(char c) 186 { 187 if((c >= 'A') && (c <= 'Z')) 188 return c - 'A'; 189 190 if((c >= 'a') && (c <= 'z')) 191 return c - 'a' + 26; 192 193 if((c >= '0') && (c <= '9')) 194 return c - '0' + 52; 195 196 if(c == '+') 197 return 62; 198 199 if(c == '/') 200 return 63; 201 202 if(c == '=') 203 return 0; 204 205 return -1; 206 } 207 } | Popular Tags |