KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > common > crypto > Base64


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.common.crypto;
20
21 import java.security.*;
22
23
24 /**
25  * Handles Base64 enconding.
26  * Inspired by the Jakarta Project
27  */

28 public class Base64
29 {
30   /**
31    * Encodes a byte array
32    *
33    * @param raw the bytes to encode
34    * @return the encoded string
35    */

36   public static String JavaDoc encode(byte[] raw)
37   {
38     int encLen = ((raw.length + 2) / 3) * 4;
39     StringBuffer JavaDoc encoded = new StringBuffer JavaDoc(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   /**
52    * Decode a string
53    *
54    * @param base64 the string to decode
55    * @return the corresponding byte array
56    */

57   public static byte[] decode(String JavaDoc 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 JavaDoc e)
90     {
91       return new byte[0];
92     }
93   }
94
95   /**
96    * Encode a Key in base64
97    *
98    * @param key the Key to encode
99    * @return the encoded String
100    */

101   public static String JavaDoc 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