KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > util > DESUtilities


1 package org.jahia.clipbuilder.html.util;
2 import java.io.UnsupportedEncodingException JavaDoc;
3 import java.security.*;
4 import java.security.spec.InvalidKeySpecException JavaDoc;
5
6 import javax.crypto.*;
7
8 /**
9  * Utilities pour crypter/décrypter
10  *
11  *@author Tlili Khaled
12  */

13 public class DESUtilities {
14     private static DESUtilities instance;
15     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DESUtilities.class);
16
17
18
19     /**
20      * Encrypte avec la clé générée
21      *
22      *@param key Description of Parameter
23      *@param message Description of Parameter
24      *@return Description of the Returned Value
25      */

26     public String JavaDoc encrypt(String JavaDoc key, String JavaDoc message) {
27         // get a encrypter object
28
DESEncrypter encrypter = new DESEncrypter();
29
30         // set the key
31
encrypter.setKey(key.getBytes());
32
33         //crypt
34
String JavaDoc encryptedMessage = encrypter.encrypt(message);
35         return encryptedMessage;
36     }
37
38
39     /**
40      * Décrypte avec la clé générée
41      *
42      *@param key Description of Parameter
43      *@param encryptedMessage Description of Parameter
44      *@return Description of the Returned Value
45      */

46     public String JavaDoc decrypt(String JavaDoc key, String JavaDoc encryptedMessage) {
47         // get a decrspter object
48
DESDecrypter decrypter = new DESDecrypter();
49
50         //set key
51
decrypter.setKey(key.getBytes());
52
53         //decrypt
54
String JavaDoc message = decrypter.decrypt(encryptedMessage);
55         return message;
56     }
57
58
59     /**
60      * Génère une clé symétrique avec DES
61      *
62      *@return Description of the Returned Value
63      */

64     public Key generateKey() {
65         try {
66             KeyGenerator kg = KeyGenerator.getInstance("DES");
67             kg.init(56);
68             // 56 is the keysize. Fixed for DES
69
Key key = kg.generateKey();
70             return key;
71         }
72         catch (NoSuchAlgorithmException ex) {
73             ex.printStackTrace();
74             return null;
75         }
76
77     }
78
79
80
81     /**
82      * Gets the Instance attribute of the DESUtilities object
83      *
84      *@return The Instance value
85      */

86     public static DESUtilities getInstance() {
87         if (instance == null) {
88             instance = new DESUtilities();
89         }
90         return instance;
91     }
92
93
94
95     /**
96      * Encrypter
97      *
98      *@author Tlili Khaled
99      */

100     class DESEncrypter {
101         private Cipher ecipher;
102
103
104         /**
105          * Constructor for the DESEncrypter object
106          */

107         public DESEncrypter() {
108
109             try {
110                 ecipher = Cipher.getInstance("DES");
111             }
112             catch (NoSuchPaddingException ex) {
113                 ex.printStackTrace();
114             }
115             catch (NoSuchAlgorithmException ex) {
116                 ex.printStackTrace();
117             }
118
119         }
120
121
122
123         /**
124          * Sets the Key attribute of the DESEncrypter object
125          *
126          *@param keyByte The new Key value
127          */

128         public void setKey(byte[] keyByte) {
129
130             try {
131                 //generate key from byte
132
javax.crypto.spec.DESKeySpec sks = new javax.crypto.spec.DESKeySpec(keyByte);
133                 SecretKey sKey = SecretKeyFactory.getInstance("DES").generateSecret(sks);
134
135                 ecipher.init(Cipher.ENCRYPT_MODE, sKey);
136             }
137             catch (NoSuchAlgorithmException ex) {
138                 ex.printStackTrace();
139             }
140             catch (InvalidKeySpecException JavaDoc ex) {
141                 ex.printStackTrace();
142             }
143             catch (InvalidKeyException ex) {
144                 ex.printStackTrace();
145             }
146
147         }
148
149
150         /**
151          * Crypte le message
152          *
153          *@param str message à crypter
154          *@return message crypté
155          */

156         public String JavaDoc encrypt(String JavaDoc str) {
157
158             try {
159                 // Encode the string into bytes using utf-8
160
byte[] utf8 = str.getBytes("UTF8");
161
162                 // Encrypt
163
byte[] enc = ecipher.doFinal(utf8);
164
165                 // Encode bytes to base64 to get a string
166
return new sun.misc.BASE64Encoder().encode(enc);
167             }
168             catch (BadPaddingException ex) {
169                 ex.printStackTrace();
170                 return null;
171             }
172             catch (IllegalBlockSizeException ex) {
173                 ex.printStackTrace();
174                 return null;
175             }
176             catch (UnsupportedEncodingException JavaDoc ex) {
177                 ex.printStackTrace();
178                 return null;
179             }
180
181         }
182
183     }
184
185
186     /**
187      * Decrytpe message
188      *
189      *@author Tlili Khaled
190      */

191     class DESDecrypter {
192         private Cipher dcipher;
193
194
195         /**
196          * Constructor for the DESEncrypter object
197          */

198         public DESDecrypter() {
199
200             try {
201                 dcipher = Cipher.getInstance("DES");
202             }
203             catch (NoSuchPaddingException ex) {
204                 ex.printStackTrace();
205             }
206             catch (NoSuchAlgorithmException ex) {
207                 ex.printStackTrace();
208             }
209
210         }
211
212
213
214         /**
215          * Sets the Key attribute of the DESEncrypter object
216          *
217          *@param keyByte The new Key value
218          */

219         public void setKey(byte[] keyByte) {
220
221             try {
222                 //generate key from byte
223
javax.crypto.spec.DESKeySpec sks = new javax.crypto.spec.DESKeySpec(keyByte);
224                 SecretKey sKey = SecretKeyFactory.getInstance("DES").generateSecret(sks);
225
226                 //set key
227
dcipher.init(Cipher.DECRYPT_MODE, sKey);
228             }
229             catch (NoSuchAlgorithmException ex) {
230                 ex.printStackTrace();
231             }
232             catch (InvalidKeySpecException JavaDoc ex) {
233                 ex.printStackTrace();
234             }
235             catch (InvalidKeyException ex) {
236                 ex.printStackTrace();
237             }
238
239         }
240
241
242
243         /**
244          * Decrypte le message
245          *
246          *@param str message à décrypter
247          *@return message décrypté
248          */

249         public String JavaDoc decrypt(String JavaDoc str) {
250             try {
251                 // Decode base64 to get bytes
252
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
253
254                 // Decrypt
255
byte[] utf8 = dcipher.doFinal(dec);
256
257                 // Decode using utf-8
258
return new String JavaDoc(utf8, "UTF8");
259             }
260             catch (javax.crypto.BadPaddingException e) {
261
262                 e.printStackTrace();
263             }
264             catch (IllegalBlockSizeException e) {
265                 e.printStackTrace();
266             }
267             catch (UnsupportedEncodingException JavaDoc e) {
268                 e.printStackTrace();
269             }
270             catch (java.io.IOException JavaDoc e) {
271                 e.printStackTrace();
272             }
273             return null;
274         }
275     }
276
277 }
278
279
Popular Tags