View Javadoc
1   /*
2    * Copyright 2013 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.v3.client.cryptor;
17  
18  import java.security.InvalidAlgorithmParameterException;
19  import java.security.InvalidKeyException;
20  import java.security.NoSuchAlgorithmException;
21  import java.security.spec.KeySpec;
22  import javax.crypto.BadPaddingException;
23  import javax.crypto.Cipher;
24  import javax.crypto.IllegalBlockSizeException;
25  import javax.crypto.KeyGenerator;
26  import javax.crypto.NoSuchPaddingException;
27  import javax.crypto.SecretKey;
28  import javax.crypto.SecretKeyFactory;
29  import javax.crypto.spec.DESedeKeySpec;
30  import org.apache.commons.codec.binary.Base64;
31  
32  /**
33   * Triple DES, 168 bit key
34   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
35   */
36  public class TripleDESCrytor implements Cryptor {
37  
38      private static final String UNICODE_FORMAT = "UTF8";
39      private static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
40      private KeySpec ks;
41      private SecretKeyFactory skf;
42      private Cipher cipher;
43      byte[] arrayBytes;
44      private String myEncryptionKey;
45      private String myEncryptionScheme;
46      SecretKey key;
47      
48      /**
49       *default constructor
50       * @throws Exception
51       */
52      public TripleDESCrytor() throws Exception {
53          String keyfromfile = CryptorFactory.loadKeyFromFile("TripleDESCrytor");
54          if (keyfromfile!=null)
55                      myEncryptionKey=keyfromfile;
56          else
57                  myEncryptionKey = "rioTEBCe/RAHRs6tTyYxDqettnVbZA6z";
58          myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
59          arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
60          ks = new DESedeKeySpec(arrayBytes);
61          skf = SecretKeyFactory.getInstance(myEncryptionScheme);
62          cipher = Cipher.getInstance(myEncryptionScheme);
63          key = skf.generateSecret(ks);
64      }
65  
66      @Override
67      public String encrypt(String clear) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
68          String encryptedString = null;
69          try {
70              cipher.init(Cipher.ENCRYPT_MODE, key);
71              byte[] plainText = clear.getBytes(UNICODE_FORMAT);
72              byte[] encryptedText = cipher.doFinal(plainText);
73              encryptedString = new String(Base64.encodeBase64(encryptedText), UNICODE_FORMAT);
74          } catch (Exception e) {
75              e.printStackTrace();
76          }
77          return encryptedString;
78      }
79  
80      /**
81       * generates a new key
82       * @return a new key
83       */
84      public static String GEN() {
85          KeyGenerator kgen;
86          try {
87              kgen = KeyGenerator.getInstance(DESEDE_ENCRYPTION_SCHEME);
88              kgen.init(168);
89              SecretKey skey = kgen.generateKey();
90              byte[] raw = skey.getEncoded();
91              return new String(Base64.encodeBase64(raw),UNICODE_FORMAT);
92          } catch (Exception ex) {
93              ex.printStackTrace();;
94          }
95          return null;
96      }
97  
98      @Override
99      public String decrypt(String str) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
100         String encryptedString = str;
101         try {
102             cipher.init(Cipher.DECRYPT_MODE, key);
103             byte[] encryptedText = Base64.decodeBase64(str.getBytes(UNICODE_FORMAT));
104             byte[] plainTest = cipher.doFinal(encryptedText);
105             encryptedString = new String(plainTest, UNICODE_FORMAT);
106         } catch (Exception e) {
107             e.printStackTrace();
108         }
109         return encryptedString;
110     }
111 
112         @Override
113         public String newKey() {
114                 return GEN();
115         }
116 }