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.KeyGenerator;
29  import javax.crypto.NoSuchPaddingException;
30  import javax.crypto.SecretKey;
31  import javax.crypto.spec.SecretKeySpec;
32  
33  /**
34   * AES bit encryption
35   *
36   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
37   */
38  public abstract class AESCryptorAbstract implements Cryptor {
39  
40      private static byte[] hexToBytes(String s) {
41          //return s.getBytes();
42          return hexToBytes(s.toCharArray());
43      }
44      private static final char[] kDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
45          'b', 'c', 'd', 'e', 'f'};
46  
47      private static byte[] hexToBytes(char[] hex) {
48          int length = hex.length / 2;
49          byte[] raw = new byte[length];
50          for (int i = 0; i < length; i++) {
51              int high = Character.digit(hex[i * 2], 16);
52              int low = Character.digit(hex[i * 2 + 1], 16);
53              int value = (high << 4) | low;
54              if (value > 127) {
55                  value -= 256;
56              }
57              raw[i] = (byte) value;
58          }
59          return raw;
60      }
61  
62      protected abstract String getKey();
63          
64      /**
65       * Constructor for DefaultCryptor.
66       */
67      public AESCryptorAbstract()
68              throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException {
69          byte[] raw =//skey.getEncoded();
70                  hexToBytes(getKey()); //
71          SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
72          // Instantiate the cipher
73          Cipher cipher = Cipher.getInstance("AES");
74          cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
75      }
76  
77      /**
78       * Encrypt the string
79       */
80      @Override
81      public String encrypt(String cleartext)
82              throws NoSuchPaddingException,
83              NoSuchAlgorithmException,
84              InvalidAlgorithmParameterException,
85              InvalidKeyException,
86              IllegalBlockSizeException,
87              BadPaddingException,
88              UnsupportedEncodingException {
89          byte[] raw = hexToBytes(getKey()); //
90          SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
91          // Instantiate the cipher
92          Cipher cipher = Cipher.getInstance("AES");
93          cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
94          byte[] encrypted = cipher.doFinal(cleartext.getBytes("UTF-8"));
95          return asHex(encrypted);
96      }
97      
98        /**
99       * Encrypt the string
100      */
101     @Override
102     public String decrypt(String str)
103             throws NoSuchPaddingException,
104             NoSuchAlgorithmException,
105             InvalidAlgorithmParameterException,
106             InvalidKeyException,
107             IllegalBlockSizeException,
108             BadPaddingException,
109             UnsupportedEncodingException{
110         byte[] raw = hexToBytes(getKey()); //
111         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
112         // Instantiate the cipher
113         Cipher cipher = Cipher.getInstance("AES");
114         cipher.init(Cipher.DECRYPT_MODE, skeySpec);
115         byte[] original = cipher.doFinal(hexToBytes(str));
116         
117         return new String(original, "UTF-8");
118     }
119 
120     private static String asHex(byte buf[]) {
121         //return new String(buf);
122         StringBuilder strbuf = new StringBuilder(buf.length * 2);
123         int i;
124 
125         for (i = 0; i < buf.length; i++) {
126             if (((int) buf[i] & 0xff) < 0x10) {
127                 strbuf.append("0");
128             }
129             strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
130         }
131 
132         return strbuf.toString();
133     }
134     
135     public static String GEN(int keysize) {
136         KeyGenerator kgen;
137         try {
138             kgen = KeyGenerator.getInstance("AES");
139             kgen.init(keysize);
140             SecretKey skey = kgen.generateKey();
141             byte[] raw = skey.getEncoded();
142             return asHex(raw);
143         } catch (Exception ex) {
144         }
145         return null;
146     }
147 }