KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > utils > CryptoUtility


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2007 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12
13 package org.openbravo.utils;
14
15 import javax.servlet.ServletException JavaDoc;
16 import javax.crypto.*;
17 import javax.crypto.spec.*;
18
19 public class CryptoUtility {
20   private static Cipher s_cipher = null;
21   private static SecretKey s_key = null;
22
23   public CryptoUtility() {
24   }
25
26   public static void main(String JavaDoc argv[]) throws Exception JavaDoc {
27     System.out.println("Enter encryption password: ");
28     System.out.flush();
29     String JavaDoc clave = argv[0];
30     System.out.println("************* " + clave);
31     String JavaDoc strEnc = CryptoUtility.encrypt(clave);
32     System.out.println("ENCRIPTED TEXT: " + strEnc);
33     System.out.println("DECRIPTED TEXT: " + CryptoUtility.decrypt(strEnc));
34   }
35
36   private static void initCipher() {
37     try {
38       s_key = new SecretKeySpec(new byte[] {
39         100, 25, 28, -122, -26, 94, -3, -72
40       }, "DES");
41       s_cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
42     } catch(Exception JavaDoc ex) {
43       ex.printStackTrace();
44     }
45   }
46
47   public static String JavaDoc encrypt(String JavaDoc value) throws ServletException JavaDoc {
48     byte encString[];
49     String JavaDoc clearText;
50     clearText = value;
51     if(clearText == null) clearText = "";
52     if(s_cipher == null) initCipher();
53     if(s_cipher == null) throw new ServletException JavaDoc("CryptoUtility.encrypt() - Can't load cipher");
54     try {
55       s_cipher.init(Cipher.ENCRYPT_MODE, s_key);
56       encString = s_cipher.doFinal(clearText.getBytes());
57     } catch (Exception JavaDoc ex) {
58       ex.printStackTrace();
59       throw new ServletException JavaDoc("CryptoUtility.encrypt() - Can't init cipher");
60     }
61     return new String JavaDoc(encString);
62   }
63
64   public static String JavaDoc decrypt(String JavaDoc value) throws ServletException JavaDoc {
65     if(value == null) return null;
66     if(value.length() == 0) return value;
67     if(s_cipher == null) initCipher();
68     if(s_cipher == null || value == null || value.length() <= 0) throw new ServletException JavaDoc("CryptoUtility.decrypt() - Can't load cipher");
69     byte out[];
70     try {
71       s_cipher.init(Cipher.DECRYPT_MODE, s_key);
72       out = s_cipher.doFinal(value.getBytes());
73     } catch (Exception JavaDoc ex) {
74       ex.printStackTrace();
75       throw new ServletException JavaDoc("CryptoUtility.decrypt() - Can't init cipher");
76     }
77     return new String JavaDoc(out);
78   }
79 }
80
Popular Tags