1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
 
public class MyKeyGenerator {
     
    public static void main(String[] args) {
        try {
            String word = "要加密的";
            String ALGORITHM="AES";
            System.out.println(byteToHexString(word.getBytes()));
            KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
            keyGenerator.init(128,new SecureRandom());  //默认是128  AES要求密钥长度为128,192,256位  
            SecretKey secretKey = keyGenerator.generateKey(); //生成密钥
            byte[] bytes = secretKey.getEncoded();
 
            String key = byteToHexString(bytes);<br>                        //String key = HexBin.encode(bytes);
            System.out.println("16进制的密钥:"+key);
            //String key2 = toHexString(bytes);
            //System.out.println(key2);
            //AES加密
            SecretKey secretKey2 = new SecretKeySpec(hexStringToBytes(key), ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey2);
            byte[] cipherByte = cipher.doFinal(word.getBytes()); //加密
             
            String result = byteToHexString(cipherByte);
            System.out.println("AES加密结果:"+result);
             
            //AES解密
            SecretKey secretKey3 = new SecretKeySpec(hexStringToBytes(key), ALGORITHM);//恢复密钥<br>                        //SecretKey secretKey2 = new SecretKeySpec(HexBin.decode(key), ALGORITHM);
            Cipher cipher2 = Cipher.getInstance(ALGORITHM);//Cipher完成加密或解密工作类
            cipher2.init(Cipher.DECRYPT_MODE, secretKey3);//对Cipher初始化,解密模式
            byte[] cipherByte2 = cipher2.doFinal(hexStringToBytes(result));//解密data
             
            System.out.println("AES解密结果:"+byteToHexString(cipherByte2));
             
            System.out.println(new String(cipherByte2));
             
             
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
 
    }
     
     /**
      * byte数组转化为16进制字符串
      * @param bytes
      * @return
      */
      public static String byteToHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
          String strHex=Integer.toHexString(bytes[i]);
          if(strHex.length() > 3) {
            sb.append(strHex.substring(6));
          } else {
            if(strHex.length() < 2) {
              sb.append("0" + strHex);
            } else {
              sb.append(strHex);
            }
          }
        }
        return sb.toString();
      }
       
      /**
       * 16进制字符串转为byte数组
       * @param hexString
       * @return
       */
      public static byte[] hexStringToBytes(String hexString) {  
            if (hexString == null || hexString.equals("")) {  
                return null;  
            }  
            hexString = hexString.toUpperCase();  
            int length = hexString.length() / 2;  
            char[] hexChars = hexString.toCharArray();  
            byte[] d = new byte[length];  
            for (int i = 0; i < length; i++) {  
                int pos = i * 2;  
                d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));  
            }  
            return d;  
        }
       
      private static byte charToByte(char c) {  
            return (byte) "0123456789ABCDEF".indexOf(c);  
      }
 
}