KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.quikj.server.framework;
2
3 import java.security.*;
4 import javax.crypto.*;
5 import javax.crypto.spec.*;
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 Encryption
12 {
13     private javax.crypto.SecretKey secretKey = null;
14     
15     /**
16      * CryptExample constructor comment.
17      */

18     public Encryption()
19     throws
20     InvalidKeyException,
21     NoSuchAlgorithmException,
22     java.security.spec.InvalidKeySpecException JavaDoc
23     {
24         super();
25         
26         Provider sun_jce = new com.sun.crypto.provider.SunJCE();
27         Security.addProvider(sun_jce);
28     }
29     /**
30      * Insert the method's description here.
31      * Creation date: (1/14/02 11:23:59 AM)
32      * @return java.lang.String
33      * @param inp byte[]
34      */

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

70     public String JavaDoc decrypt(String JavaDoc text, String JavaDoc secret)
71     {
72         try
73         {
74             DESKeySpec des_key_spec = new DESKeySpec(secret.getBytes());
75             SecretKeyFactory key_factory = SecretKeyFactory.getInstance("DES");
76             javax.crypto.SecretKey secret_key = key_factory.generateSecret(des_key_spec);
77             
78             byte[] input = stringToByte(text);
79             if (input == null)
80             {
81                 return null;
82             }
83             
84             Cipher cipher = Cipher.getInstance("DES");
85             cipher.init(Cipher.DECRYPT_MODE, secret_key);
86             byte[] decrypt_text = cipher.doFinal(input);
87             return new String JavaDoc(decrypt_text);
88         }
89         catch (Exception JavaDoc ex)
90         {
91             return null;
92         }
93     }
94     /**
95      * Insert the method's description here.
96      * Creation date: (1/2/02 12:19:43 PM)
97      * @return java.lang.String
98      * @param text java.lang.String
99      */

100     public String JavaDoc encrypt(String JavaDoc text, String JavaDoc secret)
101     {
102         try
103         {
104             byte[] secret_bytes = secret.getBytes();
105             DESKeySpec des_key_spec = new DESKeySpec(secret_bytes);
106             SecretKeyFactory key_factory = SecretKeyFactory.getInstance("DES");
107             javax.crypto.SecretKey secret_key = key_factory.generateSecret(des_key_spec);
108             
109             Cipher cipher = Cipher.getInstance("DES");
110             cipher.init(Cipher.ENCRYPT_MODE, secret_key);
111             byte[] cipher_text = cipher.doFinal(text.getBytes());
112             
113             return byteToString(cipher_text);
114         }
115         catch (Exception JavaDoc ex)
116         {
117             return null;
118         }
119     }
120     /**
121      * Insert the method's description here.
122      * Creation date: (4/12/02 5:28:07 PM)
123      * @param args java.lang.String[]
124      */

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

165     public static byte[] stringToByte(String JavaDoc text)
166     {
167         int len = text.length();
168         if ((len % 4) > 0)
169         {
170             return null;
171         }
172         
173         byte[] input = new byte[len / 4];
174         for (int i = 0; i < len; i += 4)
175         {
176             String JavaDoc element = text.substring(i, i + 4);
177             
178             if (element.startsWith("$$$") == true)
179             {
180                 element = element.substring(3);
181             }
182             else if (element.startsWith("$$") == true)
183             {
184                 element = element.substring(2);
185             }
186             else if (element.startsWith("$") == true)
187             {
188                 element = element.substring(1);
189             }
190             
191             try
192             {
193                 input[i / 4] = Byte.parseByte(element);
194             }
195             catch (NumberFormatException JavaDoc ex)
196             {
197                 return null;
198             }
199         }
200         return input;
201     }
202 }
203
Popular Tags