001: package de.webman.util.security;
002:
003: import java.security.*;
004:
005: /**
006: * Utility class for encrypting strings using a MD5 algorithm.
007: *
008: * @author <a href="mailto:gregor@webman.de">Gregor Klinke</a>
009: * @version $Revision: 1.2 $
010: **/
011: public class EncryptUtil {
012: /* $Id: EncryptUtil.java,v 1.2 2002/04/12 13:55:58 gregor Exp $ */
013:
014: /**
015: * contains the base64 char table encoding; used for the external
016: * printable form of digest md5 arrays
017: **/
018: private static final String[] BASE64_CHAR = { "A", "B", "C", "D",
019: "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
020: "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b",
021: "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
022: "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
023: "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/" };
024:
025: /**
026: * contains the rev. lookup for the base64 encoding
027: */
028: private static final int[] DEC64 = { -1, -1, -1, -1, -1, -1, -1,
029: -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */
030: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
031: -1, /* 0x10 */
032: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1,
033: 63, /* 0x20 */
034: 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1,
035: -1, /* 0x30 */
036: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 */
037: 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1,
038: -1, /* 0x50 */
039: -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
040: 40, /* 0x60 */
041: 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1,
042: -1 /* 0x70 */
043: };
044:
045: /**
046: * encodes a string to base64 encoding
047: * @param str the string to encode
048: * @return the base64 encoded string
049: **/
050: private static String encodeBase64(String str) {
051: byte[] dig = str.getBytes();
052: StringBuffer buf = new StringBuffer();
053:
054: int f0 = 0;
055: int r = 0;
056: int state = 0;
057: int j = 0;
058: int d0 = 0;
059: while (j < dig.length) {
060: switch (state) {
061: case 0:
062: d0 = (dig[j] >= 0) ? dig[j] : 256 + dig[j];
063: f0 = d0 & 0x3f;
064: r = (d0 & 0xc0) >> 6;
065: state = 1;
066: j++;
067: break;
068: case 1:
069: d0 = (dig[j] >= 0) ? dig[j] : 256 + dig[j];
070: f0 = r | (d0 & 0x0f) << 2;
071: r = (d0 & 0xf0) >> 4;
072: state = 2;
073: j++;
074: break;
075: case 2:
076: d0 = (dig[j] >= 0) ? dig[j] : 256 + dig[j];
077: f0 = r | (d0 & 0x03) << 4;
078: r = (d0 & 0xfc) >> 2;
079: state = 3;
080: j++;
081: break;
082: case 3:
083: f0 = r;
084: r = 0;
085: state = 0;
086: break;
087: }
088:
089: buf.append(BASE64_CHAR[f0]);
090: }
091:
092: if (state != 0)
093: buf.append(BASE64_CHAR[r]);
094:
095: return buf.toString();
096: }
097:
098: /**
099: * decodes a string from base64
100: * @param str the string to decode
101: * @return the decoded string
102: */
103: private static String decodeBase64(String str) {
104: byte[] dig = str.getBytes();
105: StringBuffer buf = new StringBuffer();
106: int rc = 0;
107: int r = 0;
108: int f0 = 0;
109: int d0 = 0;
110: int d1 = 0;
111: int state = 0;
112: for (int j = 0; j < dig.length; j++) {
113: switch (state) {
114: case 0:
115: r = DEC64[(dig[j] >= 0) ? dig[j] : 256 + dig[j]];
116: f0 = -1;
117: state = 1;
118: break;
119: case 1:
120: d0 = DEC64[(dig[j] >= 0) ? dig[j] : 256 + dig[j]];
121: f0 = r | ((d0 & 0x03) << 6);
122: r = (d0 & 0x3c) >> 2;
123: state = 2;
124: break;
125: case 2:
126: d0 = DEC64[(dig[j] >= 0) ? dig[j] : 256 + dig[j]];
127: f0 = r | ((d0 & 0x0f) << 4);
128: r = (d0 & 0x30) >> 4;
129: state = 3;
130: break;
131: case 3:
132: d0 = DEC64[(dig[j] >= 0) ? dig[j] : 256 + dig[j]];
133: f0 = r | (d0 << 2);
134: r = 0;
135: state = 0;
136: break;
137: }
138:
139: if (f0 > 0)
140: buf.append((char) f0);
141: }
142:
143: if (state != 0 && r > 0)
144: buf.append((char) r);
145:
146: return buf.toString();
147: }
148:
149: /**
150: * encrypts a string and returns it base64 encoded
151: * @param str the string to encode
152: * @return the base64 encoded string
153: **/
154: public static String encrypt(String str) {
155: if (str != null)
156: return encodeBase64(new Encrypter().encrypt(str));
157: return null;
158: }
159:
160: /**
161: * takes a base64 encoded, encrypted string and decodes and decrypts it
162: * @param str the string to decode
163: * @return the decrypted string
164: **/
165: public static String decrypt(String str) {
166: if (str != null)
167: return new Encrypter().decrypt(decodeBase64(str));
168: return null;
169: }
170:
171: /**
172: * the main method
173: * @args guess what
174: **/
175: public static void main(String[] args) {
176:
177: for (int i = 0; i < args.length; i++) {
178: System.out
179: .println("[" + EncryptUtil.encrypt(args[i]) + "]");
180: }
181:
182: }
183:
184: }
185:
186: class Encrypter {
187: private static final int C1 = 21777;
188: private static final int C2 = 31709;
189:
190: private int r = 42173;
191:
192: public int enc(byte b) {
193: return enc((b >= 0) ? b : 256 + b);
194: }
195:
196: public int dec(byte b) {
197: return dec((b >= 0) ? b : 256 + b);
198: }
199:
200: public int enc(int plain) {
201: int cipher = plain ^ (r >> 8);
202: r = ((cipher + r) * C1 + C2) & 0xffff;
203: return cipher;
204: }
205:
206: public int dec(int cipher) {
207: int plain = cipher ^ (r >> 8);
208: r = ((cipher + r) * C1 + C2) & 0xffff;
209: return plain;
210: }
211:
212: public String encrypt(String str) {
213: StringBuffer buf = new StringBuffer();
214: byte b[] = str.getBytes();
215:
216: for (int i = 0; i < b.length; i++) {
217: buf.append((char) enc(b[i]));
218: }
219:
220: return buf.toString();
221: }
222:
223: public String decrypt(String str) {
224: byte[] b = str.getBytes();
225: StringBuffer buf = new StringBuffer();
226:
227: for (int i = 0; i < b.length; i++) {
228: buf.append((char) dec(b[i]));
229: }
230: return buf.toString();
231: }
232: }
|