forked from gzzfw/backEnd/gz-dyh

liyj
2024-09-10 61eb0b1ca4f98c93ce03bbb58237c2d6302859cc
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
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.binary.Base64;
 
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
 
/**
 * @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";
 
    /**
     * 加密
     * @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);
        }
    }
 
    /**
     * 字节数组转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----------
 */