wjc
2023-06-28 989b4cf5a84e898e9682f8d9723a8ba1ff20c23b
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package com.hdl.sdk.link.core.utils.mqtt;
 
 
 
import com.hdl.sdk.link.common.utils.LogUtils;
import com.hdl.sdk.link.core.utils.HexUtil;
 
import org.bouncycastle.jce.provider.BouncyCastleProvider;
 
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
 
 
/**
 * Aes 加解密工具类
 *
 * @author yangtao
 * 2020年10月29日
 */
public class AesUtil {
 
    private static final String CIPHER_INSTANCE = "AES/ECB/PKCS5Padding";
    private static final String AES_ALGORITHM = "AES";
 
    static {
        //AES/CBC/PKCS7Padding 依赖
        Security.addProvider(new BouncyCastleProvider());
    }
 
    /**
     * AES加密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] aesEncrypt(byte[] content, String key) {
        return encrypt(content, key.getBytes(), "AES/CBC/PKCS7Padding", true, null);
    }
 
    /**
     * AES解密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] aesDecrypt(byte[] content, String key) {
        return decrypt(content, key.getBytes(), "AES/CBC/PKCS7Padding", true, null);
    }
 
    /**
     * 加密
     *
     * @param content 待加密字符串
     * @param keyHex  aesKye的hexStr
     * @return
     */
    public static String encrypt(String content, String keyHex) {
        return HexUtil.encodeHexStr(encrypt(content.getBytes(), HexUtil.decodeHex(keyHex), CIPHER_INSTANCE, false, null));
    }
 
    /**
     * 加密
     *
     * @param content 待加密字符串
     * @param key     密钥
     * @return
     */
    public static String encrypt(String content, byte[] key) {
        return HexUtil.encodeHexStr(encrypt(content.getBytes(), key, CIPHER_INSTANCE, false, null));
    }
 
    /**
     * 加密
     *
     * @param content 待加密字符串
     * @param keyHex  aesKye的hexStr
     * @return
     */
    public static byte[] encryptReturnByte(String content, String keyHex) {
        return encrypt(content.getBytes(), HexUtil.decodeHex(keyHex), CIPHER_INSTANCE, false, null);
    }
 
    /**
     * 加密
     *
     * @param content 待加密字符串
     * @param key     密钥
     * @return
     */
    public static byte[] encryptReturnByte(String content, byte[] key) {
        return encrypt(content.getBytes(), key, CIPHER_INSTANCE, false, null);
    }
 
    /**
     * 加密
     *
     * @param content        待加密字符串
     * @param keyByte        aesKye的hexStr
     * @param cipherInstance AES算法填充方式
     * @param isIv           是否使用偏移量 ECB模式不可用
     * @param iv             偏移量 不传默认密钥
     * @return
     */
    public static byte[] encrypt(byte[] content, byte[] keyByte, String cipherInstance, Boolean isIv, byte[] iv) {
        try {
            //KEY转换
            Key key = new SecretKeySpec(keyByte, AES_ALGORITHM);
            //加密
            Cipher cipher = Cipher.getInstance(cipherInstance);
            if (isIv != null && isIv) {
                //偏移量 不传默认密钥
                if (iv == null) {
                    iv = keyByte;
                }
                IvParameterSpec ivps = new IvParameterSpec(iv);
                cipher.init(Cipher.ENCRYPT_MODE, key, ivps);
            } else {
                cipher.init(Cipher.ENCRYPT_MODE, key);
            }
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (NoSuchAlgorithmException e) {
            LogUtils.e(e.getMessage());
        } catch (InvalidKeyException e) {
            LogUtils.e(e.getMessage());
        } catch (NoSuchPaddingException e) {
            LogUtils.e(e.getMessage());
        } catch (BadPaddingException e) {
            LogUtils.e(e.getMessage());
        } catch (IllegalBlockSizeException e) {
            LogUtils.e(e.getMessage());
        } catch (InvalidAlgorithmParameterException e) {
            LogUtils.e(e.getMessage());
        }
        return null;
    }
 
    /**
     * 解密
     *
     * @param contentHex 待解密待字符串hexStr
     * @param keyHex     desKye的hexStr
     * @return
     */
    public static String decrypt(String contentHex, String keyHex) {
        return new String(decrypt(HexUtil.decodeHex(contentHex), HexUtil.decodeHex(keyHex), CIPHER_INSTANCE, false, null));
    }
 
    /**
     * 解密
     *
     * @param contentHex 待解密待字符串hexStr
     * @param key        密钥
     * @return
     */
    public static String decrypt(String contentHex, byte[] key) {
        return new String(decrypt(HexUtil.decodeHex(contentHex), key, CIPHER_INSTANCE, false, null));
    }
 
    /**
     * 解密
     *
     * @param content 待解密待内容
     * @param keyHex  密钥
     * @return
     */
    public static String decrypt(byte[] content, String keyHex) {
        return new String(decrypt(content, HexUtil.decodeHex(keyHex), CIPHER_INSTANCE, false, null));
    }
 
    /**
     * 解密
     *
     * @param content 待解密待内容
     * @param key     密钥
     * @return
     */
    public static String decrypt(byte[] content, byte[] key) {
        return new String(decrypt(content, key, CIPHER_INSTANCE, false, null));
    }
 
 
    /**
     * 解密
     *
     * @param contentByte    待解密待字符串hexStr
     * @param contentByte    密钥
     * @param cipherInstance AES算法填充方式
     * @param isIv           是否使用偏移量 ECB模式不可用
     * @param iv             偏移量 不传默认密钥
     * @return
     */
    public static byte[] decrypt(byte[] contentByte, byte[] keyByte, String cipherInstance, Boolean isIv, byte[] iv) {
        try {
            //KEY转换
            Key key = new SecretKeySpec(keyByte, AES_ALGORITHM);
            //解密
            Cipher cipher = Cipher.getInstance(cipherInstance);
            if (isIv != null && isIv) {
                //偏移量 不传默认密钥
                if (iv == null) {
                    iv = keyByte;
                }
                IvParameterSpec ivps = new IvParameterSpec(iv);
                cipher.init(Cipher.DECRYPT_MODE, key, ivps);
            } else {
                cipher.init(Cipher.DECRYPT_MODE, key);
            }
            byte[] result = cipher.doFinal(contentByte);
            return result;
        } catch (NoSuchAlgorithmException e) {
            LogUtils.e(e.getMessage());
        } catch (InvalidKeyException e) {
            LogUtils.e(e.getMessage());
        } catch (NoSuchPaddingException e) {
            LogUtils.e(e.getMessage());
        } catch (BadPaddingException e) {
            LogUtils.e(e.getMessage());
        } catch (IllegalBlockSizeException e) {
            LogUtils.e(e.getMessage());
        } catch (InvalidAlgorithmParameterException e) {
            LogUtils.e(e.getMessage());
        }
        return null;
    }
 
 
    public static void main(String[] args) {
//        jiemi();
        //jiami();
    }
 
    public static void jiami() {
        String key = "1f6714fc-fb9b-4a";
        String content = "{\"id\":\"123\",\"objects\":[{\"sid\":\"0001011565879801020200010101\",\"type\":\"PWD\",\"local_id\":\"2\"}],\"time_stamp\":\"1603281282000\"}";
        byte[] res = aesEncrypt(content.getBytes(), key);
        System.out.println(HexUtil.encodeHexStr(res));
    }
 
    /**
     *
     * @param str
     * @param houseId
     * @return
     */
    public static String jiemi(byte[] str, String houseId) {
        byte[] rs = aesDecrypt(str, HouseIdSecretUtil.getSecret(houseId));
        String content = new String(rs);
        return content;
    }
 
//    public static void main(String[] args) {
//        try {
//            //第一步: 生成KEY
//            //KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_ALGORITHM);
//            //keyGenerator.init(256);
//            //第二步: 产生密钥
//            //SecretKey secretKey = keyGenerator.generateKey();
//            //第三步: 获取密钥
//            //byte[] keyBytes = secretKey.getEncoded();
//            //System.out.println(byteArrayToHexStr(keyBytes));
//            String src = "aaaaaaaaaVVVVaaaAAAA无";
//            String pwd = "41151AF257BFDB7859EEC62FB341EE95EE42E648FE24E1F8CE8DADE287CC1E5C";
//            String mw = encrypt(src,pwd);
//            System.out.println("mw:"+mw);
//            String jm = decrypt(mw,pwd);
//            System.out.println("jm:"+jm);
//
//        }catch (Exception e){
//
//        }
//
//    }
}