chenjiahe
2023-07-17 d4fa5269e214e8bbe1f7dd79a922cefe9fc9916b
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
116
117
118
119
120
121
122
123
124
125
package com.hx.util;
 
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
 
import org.apache.commons.codec.binary.Base64;
 
/**
 * AES加解密工具
 * 使用该类,需要行替换local_policy.jar及us_export_policy.jar
 * @author ChenJiaHe
 *
 */
public class AesUtil {
 
    /**16位加密秘钥*/
    public static final String SECRET = "huoXiong16816888";
    
    /**
     * AES加密
     * 
     * @param str
     *            需要加密的明文
     * @param secret
     *            秘钥
     * @return 加密后的密文(base64编码字符串)
     */
    public static String aesEncryp(String str, String secret) {
        if (null != str) {
            byte[] bytes = null;
 
            try {
                Base64 base64 = new Base64();
                String ALGORITHM = "AES";
                SecretKey deskey = new SecretKeySpec(secret.getBytes("UTF-8"), ALGORITHM);// 生成密钥
                Cipher c;
                c = Cipher.getInstance(ALGORITHM);
                c.init(Cipher.ENCRYPT_MODE, deskey);
                bytes = c.doFinal(str.getBytes("UTF-8"));
                if (bytes != null) {
                    return new String(base64.encode(bytes));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
 
        }
        return null;
    }
    
    /**
     * AES解密
     * 
     * @param str
     *            需要解密的秘文
     * @param secret
     *            秘钥
     * @return 解密后的明文
     */
    public static String aesDecryp(String str, String secret) {
        try {
            Base64 base64 = new Base64();
            String ALGORITHM = "AES";
            SecretKey deskey = new SecretKeySpec(secret.getBytes("UTF-8"), ALGORITHM);// 生成密钥
            Cipher c = Cipher.getInstance(ALGORITHM);
            c.init(Cipher.DECRYPT_MODE, deskey);
            return new String(c.doFinal(base64.decode(str.getBytes("UTF-8"))), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * AES加密(固定秘钥)
     *
     * @param str 需要加密的明文
     * @return 加密后的密文(base64编码字符串)
     */
    public static String aesEncryp(String str) {
        String secret = SECRET;
        if (null != str) {
            byte[] bytes = null;
 
            try {
                Base64 base64 = new Base64();
                String ALGORITHM = "AES";
                SecretKey deskey = new SecretKeySpec(secret.getBytes("UTF-8"), ALGORITHM);// 生成密钥
                Cipher c;
                c = Cipher.getInstance(ALGORITHM);
                c.init(Cipher.ENCRYPT_MODE, deskey);
                bytes = c.doFinal(str.getBytes("UTF-8"));
                if (bytes != null) {
                    return new String(base64.encode(bytes));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
 
    /**
     * AES解密(固定秘钥)
     *
     * @param str 需要解密的秘文
     * @return 解密后的明文
     */
    public static String aesDecryp(String str) {
        String secret = SECRET;
        try {
            Base64 base64 = new Base64();
            String ALGORITHM = "AES";
            SecretKey deskey = new SecretKeySpec(secret.getBytes("UTF-8"), ALGORITHM);// 生成密钥
            Cipher c = Cipher.getInstance(ALGORITHM);
            c.init(Cipher.DECRYPT_MODE, deskey);
            return new String(c.doFinal(base64.decode(str.getBytes("UTF-8"))), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
}