View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.cryptor;
17  
18  import java.security.InvalidAlgorithmParameterException;
19  import java.security.InvalidKeyException;
20  import java.security.NoSuchAlgorithmException;
21  import java.security.spec.InvalidKeySpecException;
22  
23  import javax.crypto.BadPaddingException;
24  import javax.crypto.Cipher;
25  import javax.crypto.IllegalBlockSizeException;
26  import javax.crypto.NoSuchPaddingException;
27  import javax.crypto.SecretKey;
28  import javax.crypto.SecretKeyFactory;
29  import javax.crypto.spec.PBEKeySpec;
30  import javax.crypto.spec.PBEParameterSpec;
31  
32  /***
33   * @author Anou Manavalan
34   */
35  public class DefaultCryptor implements Cryptor
36  {
37    private PBEKeySpec pbeKeySpec = null;
38    private PBEParameterSpec pbeParamSpec = null;
39    private SecretKeyFactory keyFac = null;
40    private SecretKey pbeKey = null;
41  
42    // Salt
43    private byte[] salt = {
44      (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
45      (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
46    };
47  
48    // Iteration count
49    private int count = 20;
50  
51    /***
52     * Constructor for DefaultCryptor.
53     */
54    public DefaultCryptor()
55      throws NoSuchAlgorithmException,InvalidKeySpecException
56    {
57      // Create PBE parameter set
58      pbeParamSpec = new PBEParameterSpec(salt,count);
59      pbeKeySpec = new PBEKeySpec("saagar".toCharArray());
60      keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
61      pbeKey = keyFac.generateSecret(pbeKeySpec);
62    }
63  
64    /***
65     * Encrypt the string
66     */
67    private byte[] crypt(int cipherMode,byte[] text)
68      throws  NoSuchPaddingException,
69              NoSuchAlgorithmException,
70              InvalidAlgorithmParameterException,
71              InvalidKeyException,
72              IllegalBlockSizeException,
73              BadPaddingException
74  
75    {
76      // Create PBE Cipher
77      Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
78  
79      // Initialize PBE Cipher with key and parameters
80      pbeCipher.init(cipherMode,pbeKey,pbeParamSpec);
81  
82      //byte[] text = str.getBytes();
83  
84      // Encrypt/Decrypt the string
85      byte[] cryptext = pbeCipher.doFinal(text);
86  
87      return cryptext;
88    }
89  
90    /***
91     * Encrypt the string
92     */
93    public String encrypt(String str)
94      throws  NoSuchPaddingException,
95              NoSuchAlgorithmException,
96              InvalidAlgorithmParameterException,
97              InvalidKeyException,
98              IllegalBlockSizeException,
99              BadPaddingException
100   {
101     byte[] encs = crypt(Cipher.ENCRYPT_MODE,str.getBytes());
102     return new String(encs);
103   }
104 
105   public byte[] encrypt(byte[] bytes)
106     throws  NoSuchPaddingException,
107             NoSuchAlgorithmException,
108             InvalidAlgorithmParameterException,
109             InvalidKeyException,
110             IllegalBlockSizeException,
111             BadPaddingException
112   {
113     return crypt(Cipher.ENCRYPT_MODE, bytes);
114   }
115 
116   /***
117    * Decrypt the string
118    */
119   public String decrypt(String str)
120     throws  NoSuchPaddingException,
121             NoSuchAlgorithmException,
122             InvalidAlgorithmParameterException,
123             InvalidKeyException,
124             IllegalBlockSizeException,
125             BadPaddingException
126   {
127     byte[] decs = crypt(Cipher.DECRYPT_MODE,str.getBytes());
128     return new String(decs);
129   }
130 
131 
132   public byte[] decrypt(byte[] bytes)
133     throws  NoSuchPaddingException,
134             NoSuchAlgorithmException,
135             InvalidAlgorithmParameterException,
136             InvalidKeyException,
137             IllegalBlockSizeException,
138             BadPaddingException
139   {
140     return crypt(Cipher.DECRYPT_MODE,bytes);
141   }
142 
143 
144   /****************************************************************************/
145   /****************************** TEST DRIVER *********************************/
146   /****************************************************************************/
147 
148 
149   public static void main(String[] args)
150     throws Exception
151   {
152     DefaultCryptor cryptor = new DefaultCryptor();
153     String encryptedText = cryptor.encrypt("password");
154     System.out.println("EnCrypted text [" + encryptedText + "]");
155 
156     DefaultCryptor cryptor2 = new DefaultCryptor();
157     String decryptedText = cryptor2.decrypt(encryptedText);
158     System.out.println("DeCrypted text " + decryptedText);
159   }
160 }