001: package ch.ethz.ssh2.crypto;
002:
003: import java.io.CharArrayWriter;
004: import java.io.IOException;
005:
006: /**
007: * Basic Base64 Support.
008: *
009: * @author Christian Plattner, plattner@inf.ethz.ch
010: * @version $Id: Base64.java,v 1.4 2005/08/11 12:47:31 cplattne Exp $
011: */
012: public class Base64 {
013: static final char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
014: .toCharArray();
015:
016: public static char[] encode(byte[] content) {
017: CharArrayWriter cw = new CharArrayWriter(
018: (4 * content.length) / 3);
019:
020: int idx = 0;
021:
022: int x = 0;
023:
024: for (int i = 0; i < content.length; i++) {
025: if (idx == 0)
026: x = (content[i] & 0xff) << 16;
027: else if (idx == 1)
028: x = x | ((content[i] & 0xff) << 8);
029: else
030: x = x | (content[i] & 0xff);
031:
032: idx++;
033:
034: if (idx == 3) {
035: cw.write(alphabet[x >> 18]);
036: cw.write(alphabet[(x >> 12) & 0x3f]);
037: cw.write(alphabet[(x >> 6) & 0x3f]);
038: cw.write(alphabet[x & 0x3f]);
039:
040: idx = 0;
041: }
042: }
043:
044: if (idx == 1) {
045: cw.write(alphabet[x >> 18]);
046: cw.write(alphabet[(x >> 12) & 0x3f]);
047: cw.write('=');
048: cw.write('=');
049: }
050:
051: if (idx == 2) {
052: cw.write(alphabet[x >> 18]);
053: cw.write(alphabet[(x >> 12) & 0x3f]);
054: cw.write(alphabet[(x >> 6) & 0x3f]);
055: cw.write('=');
056: }
057:
058: return cw.toCharArray();
059: }
060:
061: public static byte[] decode(char[] message) throws IOException {
062: byte buff[] = new byte[4];
063: byte dest[] = new byte[message.length];
064:
065: int bpos = 0;
066: int destpos = 0;
067:
068: for (int i = 0; i < message.length; i++) {
069: int c = message[i];
070:
071: if ((c == '\n') || (c == '\r') || (c == ' ') || (c == '\t'))
072: continue;
073:
074: if ((c >= 'A') && (c <= 'Z')) {
075: buff[bpos++] = (byte) (c - 'A');
076: } else if ((c >= 'a') && (c <= 'z')) {
077: buff[bpos++] = (byte) ((c - 'a') + 26);
078: } else if ((c >= '0') && (c <= '9')) {
079: buff[bpos++] = (byte) ((c - '0') + 52);
080: } else if (c == '+') {
081: buff[bpos++] = 62;
082: } else if (c == '/') {
083: buff[bpos++] = 63;
084: } else if (c == '=') {
085: buff[bpos++] = 64;
086: } else {
087: throw new IOException("Illegal char in base64 code.");
088: }
089:
090: if (bpos == 4) {
091: bpos = 0;
092:
093: if (buff[0] == 64)
094: break;
095:
096: if (buff[1] == 64)
097: throw new IOException(
098: "Unexpected '=' in base64 code.");
099:
100: if (buff[2] == 64) {
101: int v = (((buff[0] & 0x3f) << 6) | ((buff[1] & 0x3f)));
102: dest[destpos++] = (byte) (v >> 4);
103: break;
104: } else if (buff[3] == 64) {
105: int v = (((buff[0] & 0x3f) << 12)
106: | ((buff[1] & 0x3f) << 6) | ((buff[2] & 0x3f)));
107: dest[destpos++] = (byte) (v >> 10);
108: dest[destpos++] = (byte) (v >> 2);
109: break;
110: } else {
111: int v = (((buff[0] & 0x3f) << 18)
112: | ((buff[1] & 0x3f) << 12)
113: | ((buff[2] & 0x3f) << 6) | ((buff[3] & 0x3f)));
114: dest[destpos++] = (byte) (v >> 16);
115: dest[destpos++] = (byte) (v >> 8);
116: dest[destpos++] = (byte) (v);
117: }
118: }
119: }
120:
121: byte[] res = new byte[destpos];
122: System.arraycopy(dest, 0, res, 0, destpos);
123:
124: return res;
125: }
126: }
|