0
|
1
|
|
2 package com.trilead.ssh2.crypto;
|
|
3
|
|
4 import java.io.CharArrayWriter;
|
|
5 import java.io.IOException;
|
|
6
|
|
7 /**
|
|
8 * Basic Base64 Support.
|
|
9 *
|
|
10 * @author Christian Plattner, plattner@trilead.com
|
|
11 * @version $Id: Base64.java,v 1.1 2007/10/15 12:49:56 cplattne Exp $
|
|
12 */
|
|
13 public class Base64 {
|
|
14 static final char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
|
|
15
|
|
16 public static char[] encode(byte[] content) {
|
|
17 CharArrayWriter cw = new CharArrayWriter((4 * content.length) / 3);
|
|
18 int idx = 0;
|
|
19 int x = 0;
|
|
20
|
|
21 for (int i = 0; i < content.length; i++) {
|
|
22 if (idx == 0)
|
|
23 x = (content[i] & 0xff) << 16;
|
|
24 else if (idx == 1)
|
|
25 x = x | ((content[i] & 0xff) << 8);
|
|
26 else
|
|
27 x = x | (content[i] & 0xff);
|
|
28
|
|
29 idx++;
|
|
30
|
|
31 if (idx == 3) {
|
|
32 cw.write(alphabet[x >> 18]);
|
|
33 cw.write(alphabet[(x >> 12) & 0x3f]);
|
|
34 cw.write(alphabet[(x >> 6) & 0x3f]);
|
|
35 cw.write(alphabet[x & 0x3f]);
|
|
36 idx = 0;
|
|
37 }
|
|
38 }
|
|
39
|
|
40 if (idx == 1) {
|
|
41 cw.write(alphabet[x >> 18]);
|
|
42 cw.write(alphabet[(x >> 12) & 0x3f]);
|
|
43 cw.write('=');
|
|
44 cw.write('=');
|
|
45 }
|
|
46
|
|
47 if (idx == 2) {
|
|
48 cw.write(alphabet[x >> 18]);
|
|
49 cw.write(alphabet[(x >> 12) & 0x3f]);
|
|
50 cw.write(alphabet[(x >> 6) & 0x3f]);
|
|
51 cw.write('=');
|
|
52 }
|
|
53
|
|
54 return cw.toCharArray();
|
|
55 }
|
|
56
|
|
57 public static byte[] decode(char[] message) throws IOException {
|
|
58 byte buff[] = new byte[4];
|
|
59 byte dest[] = new byte[message.length];
|
|
60 int bpos = 0;
|
|
61 int destpos = 0;
|
|
62
|
|
63 for (int i = 0; i < message.length; i++) {
|
|
64 int c = message[i];
|
|
65
|
|
66 if ((c == '\n') || (c == '\r') || (c == ' ') || (c == '\t'))
|
|
67 continue;
|
|
68
|
|
69 if ((c >= 'A') && (c <= 'Z')) {
|
|
70 buff[bpos++] = (byte)(c - 'A');
|
|
71 }
|
|
72 else if ((c >= 'a') && (c <= 'z')) {
|
|
73 buff[bpos++] = (byte)((c - 'a') + 26);
|
|
74 }
|
|
75 else if ((c >= '0') && (c <= '9')) {
|
|
76 buff[bpos++] = (byte)((c - '0') + 52);
|
|
77 }
|
|
78 else if (c == '+') {
|
|
79 buff[bpos++] = 62;
|
|
80 }
|
|
81 else if (c == '/') {
|
|
82 buff[bpos++] = 63;
|
|
83 }
|
|
84 else if (c == '=') {
|
|
85 buff[bpos++] = 64;
|
|
86 }
|
|
87 else {
|
|
88 throw new IOException("Illegal char in base64 code.");
|
|
89 }
|
|
90
|
|
91 if (bpos == 4) {
|
|
92 bpos = 0;
|
|
93
|
|
94 if (buff[0] == 64)
|
|
95 break;
|
|
96
|
|
97 if (buff[1] == 64)
|
|
98 throw new IOException("Unexpected '=' in base64 code.");
|
|
99
|
|
100 if (buff[2] == 64) {
|
|
101 int v = (((buff[0] & 0x3f) << 6) | ((buff[1] & 0x3f)));
|
|
102 dest[destpos++] = (byte)(v >> 4);
|
|
103 break;
|
|
104 }
|
|
105 else if (buff[3] == 64) {
|
|
106 int v = (((buff[0] & 0x3f) << 12) | ((buff[1] & 0x3f) << 6) | ((buff[2] & 0x3f)));
|
|
107 dest[destpos++] = (byte)(v >> 10);
|
|
108 dest[destpos++] = (byte)(v >> 2);
|
|
109 break;
|
|
110 }
|
|
111 else {
|
|
112 int v = (((buff[0] & 0x3f) << 18) | ((buff[1] & 0x3f) << 12) | ((buff[2] & 0x3f) << 6) | ((buff[3] & 0x3f)));
|
|
113 dest[destpos++] = (byte)(v >> 16);
|
|
114 dest[destpos++] = (byte)(v >> 8);
|
|
115 dest[destpos++] = (byte)(v);
|
|
116 }
|
|
117 }
|
|
118 }
|
|
119
|
|
120 byte[] res = new byte[destpos];
|
|
121 System.arraycopy(dest, 0, res, 0, destpos);
|
|
122 return res;
|
|
123 }
|
|
124 }
|