View Javadoc
1   /*
2    * Copyright 2001-2008 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   */
17  package org.apache.juddi.v3.client.cryptor;
18  
19  import java.io.UnsupportedEncodingException;
20  import java.security.InvalidAlgorithmParameterException;
21  import java.security.InvalidKeyException;
22  import java.security.NoSuchAlgorithmException;
23  import java.security.spec.InvalidKeySpecException;
24  
25  import javax.crypto.BadPaddingException;
26  import javax.crypto.Cipher;
27  import javax.crypto.IllegalBlockSizeException;
28  import javax.crypto.NoSuchPaddingException;
29  import javax.crypto.SecretKey;
30  import javax.crypto.SecretKeyFactory;
31  import javax.crypto.spec.PBEKeySpec;
32  import javax.crypto.spec.PBEParameterSpec;
33  
34  import org.apache.commons.codec.binary.Base64;
35  
36  /**
37   * @author Anou Manavalan
38   */
39  public class DefaultCryptor implements Cryptor {
40  
41          private PBEKeySpec pbeKeySpec = null;
42          private PBEParameterSpec pbeParamSpec = null;
43          private SecretKeyFactory keyFac = null;
44          private SecretKey pbeKey = null;
45  
46          // Salt
47          private byte[] salt = {
48                  (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
49                  (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99
50          };
51  
52          // Iteration count
53          private int count = 20;
54  
55          /**
56           * Constructor for DefaultCryptor.
57           */
58          public DefaultCryptor()
59               throws NoSuchAlgorithmException, InvalidKeySpecException {
60                  // Create PBE parameter set
61                  pbeParamSpec = new PBEParameterSpec(salt, count);
62                  pbeKeySpec = new PBEKeySpec("saagar".toCharArray());
63                  keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
64                  pbeKey = keyFac.generateSecret(pbeKeySpec);
65          }
66  
67          /**
68           * Encrypt the string
69           */
70          private byte[] crypt(int cipherMode, byte[] text)
71               throws NoSuchPaddingException,
72               NoSuchAlgorithmException,
73               InvalidAlgorithmParameterException,
74               InvalidKeyException,
75               IllegalBlockSizeException,
76               BadPaddingException {
77                  // Create PBE Cipher
78                  Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
79  
80                  // Initialize PBE Cipher with key and parameters
81                  pbeCipher.init(cipherMode, pbeKey, pbeParamSpec);
82  
83      //byte[] text = str.getBytes();
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              UnsupportedEncodingException {
101                 byte[] encs = crypt(Cipher.ENCRYPT_MODE, str.getBytes());
102                 encs = Base64.encodeBase64(encs);
103                 return new String(encs, "UTF-8");
104         }
105 
106         public String decrypt(String str) throws NoSuchPaddingException,
107              NoSuchAlgorithmException,
108              InvalidAlgorithmParameterException,
109              InvalidKeyException,
110              IllegalBlockSizeException,
111              BadPaddingException,
112              UnsupportedEncodingException {
113                 byte[] encs = crypt(Cipher.DECRYPT_MODE, Base64.decodeBase64(str.getBytes("UTF-8")));
114                 return new String(encs, "UTF-8");
115         }
116 
117         @Override
118         public String newKey() {
119                 throw new UnsupportedOperationException("Not supported yet."); 
120         }
121 
122 }