KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > DesEncryptionHelper


1 package org.infoglue.cms.util;
2
3 import java.io.UnsupportedEncodingException JavaDoc;
4
5 import javax.crypto.Cipher;
6 import javax.crypto.IllegalBlockSizeException;
7 import javax.crypto.KeyGenerator;
8 import javax.crypto.SecretKey;
9
10 /* ===============================================================================
11 *
12 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
13 *
14 * ===============================================================================
15 *
16 * Copyright (C)
17 *
18 * This program is free software; you can redistribute it and/or modify it under
19 * the terms of the GNU General Public License version 2, as published by the
20 * Free Software Foundation. See the file LICENSE.html for more information.
21 *
22 * This program is distributed in the hope that it will be useful, but WITHOUT
23 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
24 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License along with
27 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
28 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
29 *
30 * ===============================================================================
31 */

32
33 public class DesEncryptionHelper
34 {
35     private static SecretKey key = null;
36     
37     static
38     {
39         try
40         {
41             // Generate a temporary key. In practice, you would save this key.
42
// See also e464 Encrypting with DES Using a Pass Phrase.
43
key = KeyGenerator.getInstance("DES").generateKey();
44         }
45         catch (Exception JavaDoc e)
46         {
47         }
48     }
49
50     Cipher ecipher;
51     Cipher dcipher;
52
53     public DesEncryptionHelper()
54     {
55         try
56         {
57             ecipher = Cipher.getInstance("DES");
58             dcipher = Cipher.getInstance("DES");
59             ecipher.init(Cipher.ENCRYPT_MODE, key);
60             dcipher.init(Cipher.DECRYPT_MODE, key);
61
62         }
63         catch (javax.crypto.NoSuchPaddingException e)
64         {
65         }
66         catch (java.security.NoSuchAlgorithmException JavaDoc e)
67         {
68         }
69         catch (java.security.InvalidKeyException JavaDoc e)
70         {
71         }
72     }
73
74     public String JavaDoc encrypt(String JavaDoc str)
75     {
76         try
77         {
78             // Encode the string into bytes using utf-8
79
byte[] utf8 = str.getBytes("UTF8");
80
81             // Encrypt
82
byte[] enc = ecipher.doFinal(utf8);
83
84             // Encode bytes to base64 to get a string
85
return new sun.misc.BASE64Encoder().encode(enc);
86         }
87         catch (javax.crypto.BadPaddingException e)
88         {
89         }
90         catch (IllegalBlockSizeException e)
91         {
92         }
93         catch (UnsupportedEncodingException JavaDoc e)
94         {
95         }
96         catch (java.io.IOException JavaDoc e)
97         {
98         }
99         
100         return null;
101     }
102
103     public String JavaDoc decrypt(String JavaDoc str)
104     {
105         try
106         {
107             // Decode base64 to get bytes
108
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
109
110             // Decrypt
111
byte[] utf8 = dcipher.doFinal(dec);
112
113             // Decode using utf-8
114
return new String JavaDoc(utf8, "UTF8");
115         }
116         catch (javax.crypto.BadPaddingException e)
117         {
118         }
119         catch (IllegalBlockSizeException e)
120         {
121         }
122         catch (UnsupportedEncodingException JavaDoc e)
123         {
124         }
125         catch (java.io.IOException JavaDoc e)
126         {
127         }
128         
129         return null;
130     }
131 }
Popular Tags