- 论坛徽章:
- 0
|
原文:
http://blog.csdn.net/zyg158/archive/2007/06/26/1667531.aspx
public class Base64 {
private static final byte[] encodingTable = {
(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E',
(byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
(byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O',
(byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
(byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y',
(byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
(byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i',
(byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
(byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's',
(byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
(byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2',
(byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
(byte) '8', (byte) '9', (byte) '+', (byte) '/'
};
private static final byte[] decodingTable;
static {
decodingTable = new byte[128];
for (int i = 0; i >> 2) & 0x3f];
bytes[j + 1] = encodingTable[((a1 >> 4)) & 0x3f];
bytes[j + 2] = encodingTable[((a2 >> 6)) & 0x3f];
bytes[j + 3] = encodingTable[a3 & 0x3f];
}
int b1;
int b2;
int b3;
int d1;
int d2;
switch (modulus) {
case 0: /* nothing left to do */
break;
case 1:
d1 = data[data.length - 1] & 0xff;
b1 = (d1 >>> 2) & 0x3f;
b2 = (d1 >> 2) & 0x3f;
b2 = ((d1 >> 4)) & 0x3f;
b3 = (d2 > 4));
bytes[j + 1] = (byte) ((b2 > 2));
bytes[j + 2] = (byte) ((b3 > 4));
} else if (data[data.length - 1] == '=') {
b1 = decodingTable[data[data.length - 4]];
b2 = decodingTable[data[data.length - 3]];
b3 = decodingTable[data[data.length - 2]];
bytes[bytes.length - 2] = (byte) ((b1 > 4));
bytes[bytes.length - 1] = (byte) ((b2 > 2));
} else {
b1 = decodingTable[data[data.length - 4]];
b2 = decodingTable[data[data.length - 3]];
b3 = decodingTable[data[data.length - 2]];
b4 = decodingTable[data[data.length - 1]];
bytes[bytes.length - 3] = (byte) ((b1 > 4));
bytes[bytes.length - 2] = (byte) ((b2 > 2));
bytes[bytes.length - 1] = (byte) ((b3 > 4));
bytes[j + 1] = (byte) ((b2 > 2));
bytes[j + 2] = (byte) ((b3 > 4));
} else if (data.charAt(data.length() - 1) == '=') {
b1 = decodingTable[data.charAt(data.length() - 4)];
b2 = decodingTable[data.charAt(data.length() - 3)];
b3 = decodingTable[data.charAt(data.length() - 2)];
bytes[bytes.length - 2] = (byte) ((b1 > 4));
bytes[bytes.length - 1] = (byte) ((b2 > 2));
} else {
b1 = decodingTable[data.charAt(data.length() - 4)];
b2 = decodingTable[data.charAt(data.length() - 3)];
b3 = decodingTable[data.charAt(data.length() - 2)];
b4 = decodingTable[data.charAt(data.length() - 1)];
bytes[bytes.length - 3] = (byte) ((b1 > 4));
bytes[bytes.length - 2] = (byte) ((b2 > 2));
bytes[bytes.length - 1] = (byte) ((b3 = 128)) {
return false;
} else if (decodingTable == -1) {
return false;
}
return true;
}
public static void main(String[] args) {
String data = "1";
byte[] result = Base64.encode(data.getBytes());
System.out.println(data);
System.out.println(new String(result));
System.out.println(new String(Base64.decode(new String(result))));
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/40668/showart_330068.html |
|