This project has retired. For details please refer to its Attic page.
AES xref
View Javadoc
1   /*
2    * Copyright 2001-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   */
17  package org.apache.juddi.samples;
18  
19  import javax.crypto.Cipher;
20  import javax.crypto.KeyGenerator;
21  import javax.crypto.SecretKey;
22  import javax.crypto.spec.SecretKeySpec;
23  
24  /**
25   * This call is a simple test class that shows how to encrypt stuff in Java
26   * using AES
27   *
28   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
29   */
30  public class AES {
31  
32          public static String GEN(int keysize) {
33                  KeyGenerator kgen;
34                  try {
35                          kgen = KeyGenerator.getInstance("AES");
36                          kgen.init(keysize);
37                          SecretKey skey = kgen.generateKey();
38                          byte[] raw = skey.getEncoded();
39                          return asHex(raw);
40                  } catch (Exception ex) {
41                  }
42                  return null;
43          }
44  
45          private static String asHex(byte buf[]) {
46                  //return new String(buf);
47                  StringBuilder strbuf = new StringBuilder(buf.length * 2);
48                  int i;
49  
50                  for (i = 0; i < buf.length; i++) {
51                          if (((int) buf[i] & 0xff) < 0x10) {
52                                  strbuf.append("0");
53                          }
54                          strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
55                  }
56  
57                  return strbuf.toString();
58          }
59  
60          private static byte[] hexToBytes(String s) {
61                  //return s.getBytes();
62                  return hexToBytes(s.toCharArray());
63          }
64          private static final char[] kDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
65                  'b', 'c', 'd', 'e', 'f'};
66  
67          private static byte[] hexToBytes(char[] hex) {
68                  int length = hex.length / 2;
69                  byte[] raw = new byte[length];
70                  for (int i = 0; i < length; i++) {
71                          int high = Character.digit(hex[i * 2], 16);
72                          int low = Character.digit(hex[i * 2 + 1], 16);
73                          int value = (high << 4) | low;
74                          if (value > 127) {
75                                  value -= 256;
76                          }
77                          raw[i] = (byte) value;
78                  }
79                  return raw;
80          }
81          //default key
82          // private final static String something2 = "dde284c781d60ca0b56c4b23eec85217951dc99869402abd42c7dcc9080d60aa";
83  
84          public static void main(String[] args) throws Exception {
85                  //ee4bd3eefe38c3d996a89589de5b9698
86                  String key = GEN(128);
87                  System.out.println(key);
88  
89                  byte[] raw = hexToBytes(key); //
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("password".getBytes("UTF-8"));
95                  String enc = (asHex(encrypted));
96                  System.out.println(enc);
97  
98                  skeySpec = new SecretKeySpec(hexToBytes(key), "AES");
99                  cipher.init(Cipher.DECRYPT_MODE, skeySpec);
100                 byte[] original = cipher.doFinal(hexToBytes(enc));
101 
102                 System.out.println(new String(original, "UTF-8"));
103 
104         }
105 }