chenjiahe
2022-12-16 5bfcd30741d716cb46bb4540051c3bed65737189
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import javax.crypto.Cipher;
4 import javax.crypto.SecretKey;
5 import javax.crypto.spec.SecretKeySpec;
6
7 import org.apache.commons.codec.binary.Base64;
8
9 /**
10  * AES加解密工具
11  * 使用该类,需要行替换local_policy.jar及us_export_policy.jar
12  * @author ChenJiaHe
13  *
14  */
15 public class AesUtil {
16
17     /**16位加密秘钥*/
18     public static final String SECRET = "huoXiong16816888";
19     
20     /**
21      * AES加密
22      * 
23      * @param str
24      *            需要加密的明文
25      * @param secret
26      *            秘钥
27      * @return 加密后的密文(base64编码字符串)
28      */
29     public static String aesEncryp(String str, String secret) {
30         if (null != str) {
31             byte[] bytes = null;
32
33             try {
34                 Base64 base64 = new Base64();
35                 String ALGORITHM = "AES";
36                 SecretKey deskey = new SecretKeySpec(secret.getBytes("UTF-8"), ALGORITHM);// 生成密钥
37                 Cipher c;
38                 c = Cipher.getInstance(ALGORITHM);
39                 c.init(Cipher.ENCRYPT_MODE, deskey);
40                 bytes = c.doFinal(str.getBytes("UTF-8"));
41                 if (bytes != null) {
42                     return new String(base64.encode(bytes));
43                 }
44             } catch (Exception e) {
45                 e.printStackTrace();
46             }
47
48         }
49         return null;
50     }
51     
52     /**
53      * AES解密
54      * 
55      * @param str
56      *            需要解密的秘文
57      * @param secret
58      *            秘钥
59      * @return 解密后的明文
60      */
61     public static String aesDecryp(String str, String secret) {
62         try {
63             Base64 base64 = new Base64();
64             String ALGORITHM = "AES";
65             SecretKey deskey = new SecretKeySpec(secret.getBytes("UTF-8"), ALGORITHM);// 生成密钥
66             Cipher c = Cipher.getInstance(ALGORITHM);
67             c.init(Cipher.DECRYPT_MODE, deskey);
68             return new String(c.doFinal(base64.decode(str.getBytes("UTF-8"))), "UTF-8");
69         } catch (Exception e) {
70             e.printStackTrace();
71         }
72         return null;
73     }
74
75     /**
76      * AES加密(固定秘钥)
77      *
78      * @param str 需要加密的明文
79      * @return 加密后的密文(base64编码字符串)
80      */
81     public static String aesEncryp(String str) {
82         String secret = SECRET;
83         if (null != str) {
84             byte[] bytes = null;
85
86             try {
87                 Base64 base64 = new Base64();
88                 String ALGORITHM = "AES";
89                 SecretKey deskey = new SecretKeySpec(secret.getBytes("UTF-8"), ALGORITHM);// 生成密钥
90                 Cipher c;
91                 c = Cipher.getInstance(ALGORITHM);
92                 c.init(Cipher.ENCRYPT_MODE, deskey);
93                 bytes = c.doFinal(str.getBytes("UTF-8"));
94                 if (bytes != null) {
95                     return new String(base64.encode(bytes));
96                 }
97             } catch (Exception e) {
98                 e.printStackTrace();
99             }
100         }
101         return null;
102     }
103
104     /**
105      * AES解密(固定秘钥)
106      *
107      * @param str 需要解密的秘文
108      * @return 解密后的明文
109      */
110     public static String aesDecryp(String str) {
111         String secret = SECRET;
112         try {
113             Base64 base64 = new Base64();
114             String ALGORITHM = "AES";
115             SecretKey deskey = new SecretKeySpec(secret.getBytes("UTF-8"), ALGORITHM);// 生成密钥
116             Cipher c = Cipher.getInstance(ALGORITHM);
117             c.init(Cipher.DECRYPT_MODE, deskey);
118             return new String(c.doFinal(base64.decode(str.getBytes("UTF-8"))), "UTF-8");
119         } catch (Exception e) {
120             e.printStackTrace();
121         }
122         return null;
123     }
124
125 }