广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package cn.huge.base.common.utils;
 
import cn.huge.base.common.exception.BaseException;
import com.thoughtworks.xstream.core.util.Base64Encoder;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
 
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
 
/**
 * @title: aes加密工具
 * @description: aes加密工具
 * @company: hugeinfo
 * @author: liyj
 * @time: 2021-11-05 16:51:48
 * @version: 1.0.0
 */
public class AesUtils {
    /**
     * aes加密秘钥
     */
    public static final String AES_PASSWORD = "tVTShp12fFVkNBxV";
    /**
     * 默认的加密方式
     */
    public static final String KEY_ALGORITHM = "AES";
    /**
     * 默认的加密算法
     */
    public static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
 
    private static final String iv = "1qaz#EDC123456(,";
    private static final String Algorithm = "AES";
    private static final String AlgorithmProvider = "AES/CBC/PKCS5Padding";
 
    /**
     * 加密
     *
     * @param content
     * @return
     * @throws Exception
     */
    public static String encode(String content) {
        try {
            byte[] raw = AES_PASSWORD.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            //"算法/模式/补码方式"
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(content.getBytes("utf-8"));
            return byte2Base64(encrypted);
        } catch (Exception e) {
            throw new BaseException("AesUtils.encodeByte", e);
        }
    }
 
    /**
     * 加密
     *
     * @param content
     * @return
     * @throws Exception
     */
    public static byte[] encodeByte(String content) {
        try {
            byte[] raw = AES_PASSWORD.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            //"算法/模式/补码方式"
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(content.getBytes("utf-8"));
            return encrypted;
        } catch (Exception e) {
            throw new BaseException("AesUtils.encodeByte", e);
        }
    }
 
    /**
     * 加密
     *
     * @param data 返回数据
     * @return ReturnBo
     * @throws Exception
     */
    public static byte[] encodeByte(Object data) {
        try {
            //加密
            byte[] raw = AES_PASSWORD.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            JsonMapper jsonMapper = new JsonMapper();
            byte[] encrypted = cipher.doFinal(jsonMapper.toJson(data).getBytes("utf-8"));
            return encrypted;
        } catch (Exception e) {
            throw new BaseException("AesUtils.encodeByte", e);
        }
    }
 
    /**
     * 解密
     *
     * @param content
     * @return
     * @throws Exception
     */
    public static String decodeByte(byte[] content) {
        try {
            byte[] raw = AES_PASSWORD.getBytes();
            SecretKeySpec key = new SecretKeySpec(raw, KEY_ALGORITHM);
            //创建密码器
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(content);
            //AES解密
            return new String(result);
        } catch (Exception e) {
            throw new BaseException("AesUtils.decodeByte", e);
        }
    }
 
    /**
     * 解密
     *
     * @param content
     * @return
     * @throws Exception
     */
    public static String decode(String content) {
        try {
            byte[] raw = AES_PASSWORD.getBytes();
            SecretKeySpec key = new SecretKeySpec(raw, KEY_ALGORITHM);
            //创建密码器
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, key);
            //base64解密
            byte[] contentByte = new Base64().decode(content);
            byte[] result = cipher.doFinal(contentByte);
            //AES解密
            return new String(result, "utf-8");
        } catch (Exception e) {
            throw new BaseException("AesUtils.decode", e);
        }
    }
 
 
    public static String decrypt(String enc, String uniqueKey) throws NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidAlgorithmParameterException, InvalidKeyException, DecoderException, IllegalBlockSizeException, BadPaddingException {
        byte[] key = uniqueKey.getBytes();
        SecretKey secretKey = new SecretKeySpec(key, Algorithm);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes("utf-8"));
        Cipher cipher = Cipher.getInstance(AlgorithmProvider);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
        byte[] hexBytes = hexStringToBytes(enc);
        byte[] plainBytes = cipher.doFinal(hexBytes);
        return new String(plainBytes, "UTF-8");
    }
 
    public static String encrypt(String plainText, String uniqueKey) throws NoSuchPaddingException, NoSuchAlgorithmException,
            UnsupportedEncodingException, InvalidAlgorithmParameterException, InvalidKeyException,
            IllegalBlockSizeException, BadPaddingException {
        byte[] key = uniqueKey.getBytes();
        SecretKey secretKey = new SecretKeySpec(key, Algorithm);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes("utf-8"));
        Cipher cipher = Cipher.getInstance(AlgorithmProvider);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
        return Hex.encodeHexString(encryptedBytes);
    }
 
    /**
     * 将16进制字符串转换为byte数组
     *
     * @param hexString
     * @return
     */
    private static byte[] hexStringToBytes(String hexString) throws DecoderException {
        return Hex.decodeHex(hexString);
    }
 
 
    /**
     * 字节数组转Base64编码
     *
     * @param bytes
     * @return
     */
    public static String byte2Base64(byte[] bytes) {
        Base64Encoder encoder = new Base64Encoder();
        return encoder.encode(bytes);
    }
}
/**
 * -------------------_ooOoo_-------------------
 * ------------------o8888888o------------------
 * ------------------88" . "88------------------
 * ------------------(| -_- |)------------------
 * ------------------O\  =  /O------------------
 * ---------------____/`---'\____---------------
 * -------------.'  \\|     |//  `.-------------
 * ------------/  \\|||  :  |||//  \------------
 * -----------/  _||||| -:- |||||-  \-----------
 * -----------|   | \\\  -  /// |   |-----------
 * -----------| \_|  ''\---/''  |   |-----------
 * -----------\  .-\__  `-`  ___/-. /-----------
 * ---------___`. .'  /--.--\  `. . __----------
 * ------."" '<  `.___\_<|>_/___.'  >'"".-------
 * -----| | :  `- \`.;`\ _ /`;.`/ - ` : | |-----
 * -----\  \ `-.   \_ __\ /__ _/   .-` /  /-----
 * ======`-.____`-.___\_____/___.-`____.-'======
 * -------------------`=---='
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 * ---------佛祖保佑---hugeinfo---永无BUG----------
 */