KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > server > framework > DESEncryption


1 package com.quikj.server.framework;
2
3 import lava.security.des.*;
4 import java.util.*;
5
6 /**
7  * Insert the type's description here.
8  * Creation date: (1/2/02 12:18:02 PM)
9  * @author:
10  */

11 public final class DESEncryption
12 {
13     private static final String JavaDoc PAD8 = " ";
14     
15     /**
16      * CryptExample constructor comment.
17      */

18     public DESEncryption()
19     {
20     }
21     /**
22      * Insert the method's description here.
23      * Creation date: (1/14/02 11:23:59 AM)
24      * @return java.lang.String
25      * @param inp byte[]
26      */

27     public static String JavaDoc byteToString(byte[] inp)
28     {
29         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
30         for (int i = 0; i < inp.length; i++)
31         {
32             String JavaDoc n = Byte.toString(inp[i]);
33             int len = n.length();
34             
35             if (len == 1)
36             {
37                 buffer.append("$$$" + n);
38             }
39             else if (len == 2)
40             {
41                 buffer.append("$$" + n);
42             }
43             else if (len == 3)
44             {
45                 buffer.append("$" + n);
46             }
47             else
48             {
49                 buffer.append(n);
50             }
51         }
52         
53         return buffer.toString();
54         
55     }
56     /**
57      * Insert the method's description here.
58      * Creation date: (1/2/02 2:15:43 PM)
59      * @return java.lang.String
60      * @param text java.lang.String
61      */

62     public String JavaDoc decode(String JavaDoc text, String JavaDoc secret)
63     {
64         try
65         {
66             byte[] input = stringToByte(text);
67             if (input == null)
68             {
69                 return null;
70             }
71             
72             byte[] output = new byte[input.length];
73             
74             DesKey des_key = new DesKey(secret.getBytes(), 0);
75             DesEngine engine = new DesEngine(des_key);
76             
77             engine.decrypt(input, output, input.length);
78             return new String JavaDoc(output);
79         }
80         catch (Exception JavaDoc ex)
81         {
82             return null;
83         }
84     }
85     /**
86      * Insert the method's description here.
87      * Creation date: (1/2/02 12:19:43 PM)
88      * @return java.lang.String
89      * @param text java.lang.String
90      */

91     public String JavaDoc encode(String JavaDoc text, String JavaDoc secret)
92     {
93         try
94         {
95             int pad_length = 8 - (text.length() % 8);
96             
97             String JavaDoc padded_text = text + PAD8.substring(0, pad_length);
98             byte[] input = padded_text.getBytes();
99             
100             byte[] output = new byte[input.length];
101             
102             DesKey des_key = new DesKey(secret.getBytes(), 0);
103             DesEngine engine = new DesEngine(des_key);
104             
105             engine.encrypt(input, output, text.length());
106             return byteToString(output);
107         }
108         catch (Exception JavaDoc ex)
109         {
110             return null;
111         }
112     }
113     /**
114      * Insert the method's description here.
115      * Creation date: (4/12/02 5:28:07 PM)
116      * @param args java.lang.String[]
117      */

118     public static void main(String JavaDoc[] args)
119     {
120         try
121         {
122             if (args.length < 3)
123             {
124                 System.err.println("Usage <-e/-d> text secret");
125                 System.exit(1);
126             }
127             
128             DESEncryption enc = new DESEncryption();
129             if (args[0].equals("-e") == true)
130             {
131                 System.out.println("Result: " + enc.encrypt(args[1], args[2]));
132                 
133             }
134             else if (args[0].equals("-d") == true)
135             {
136                 System.out.println("Result: --" + enc.decrypt(args[1], args[2])
137                 +"--");
138             }
139             else
140             {
141                 System.err.println("Unknown option " + args[0]);
142                 System.exit(2);
143             }
144         }
145         catch (Exception JavaDoc ex)
146         {
147             System.err.println(ex.getClass().getName() + ": " + ex.getMessage());
148             System.exit(3);
149         }
150         
151         System.exit(0);
152     }
153     /**
154      * Insert the method's description here.
155      * Creation date: (1/14/02 11:20:06 AM)
156      * @return byte[]
157      * @param text java.lang.String
158      */

159     public static byte[] stringToByte(String JavaDoc text)
160     {
161         int len = text.length();
162         if ((len % 4) > 0)
163         {
164             return null;
165         }
166         
167         byte[] input = new byte[len / 4];
168         for (int i = 0; i < len; i += 4)
169         {
170             String JavaDoc element = text.substring(i, i + 4);
171             
172             if (element.startsWith("$$$") == true)
173             {
174                 element = element.substring(3);
175             }
176             else if (element.startsWith("$$") == true)
177             {
178                 element = element.substring(2);
179             }
180             else if (element.startsWith("$") == true)
181             {
182                 element = element.substring(1);
183             }
184             
185             try
186             {
187                 input[i / 4] = Byte.parseByte(element);
188             }
189             catch (NumberFormatException JavaDoc ex)
190             {
191                 return null;
192             }
193         }
194         return input;
195     }
196     
197     public String JavaDoc encrypt(String JavaDoc text, String JavaDoc secret)
198     {
199         return text.length() + ":" + encode(text, secret);
200     }
201     
202     public String JavaDoc decrypt(String JavaDoc text, String JavaDoc secret)
203     {
204         StringTokenizer tokens = new StringTokenizer(text, ":");
205         int num_tokens = tokens.countTokens();
206         
207         if (num_tokens < 2)
208         {
209             return null;
210         }
211         
212         String JavaDoc size_s = tokens.nextToken();
213         int size = 0;
214         try
215         {
216             size = Integer.parseInt(size_s);
217         }
218         catch (NumberFormatException JavaDoc ex)
219         {
220             return null;
221         }
222         
223         String JavaDoc encrypted_message = text.substring(size_s.length() + 1);
224         String JavaDoc decrypted_str = decode(encrypted_message, secret);
225         if (decrypted_str == null)
226         {
227             return null;
228         }
229         
230         return decrypted_str.substring(0, size);
231     }
232 }
233
Popular Tags