AESEncryptUtils.java
3.83 KB
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
package com.glsx.libble.util;
import com.blankj.utilcode.util.LogUtils;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* @Package: com.glsx.ble_module.util
* @ClassName: AESUtil
* @Company GLSX
* @Email yangks@didihu.com.cn
* @Description: 作用描述:
* @Author: yangks
* @CreateDate: 2021/4/16 9:32
* @Version:
*/
public class AESEncryptUtils {
//秘钥算法
private static final String KEY_ALGORITHM = "AES";
//加密算法:algorithm/mode/padding 算法/工作模式/填充模式
private static final String CIPHER_ALGORITHM = "AES/CBC/NoPadding";
//秘钥
//private static final String KEY = "12345678";//DES 秘钥长度必须是8 位或以上
//private static String KEY = "1234567890123456";//AES 秘钥长度必须是16 位
//加密
public static byte[] encryption(byte[] mingwen, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
if (key.length() != 16) {
LogUtils.eTag("TAG", "key is err");
return "keylength is error".getBytes();
}
//将明文填充0补足128位
byte[] tcmingwen = new byte[16];
System.arraycopy(mingwen, 0, tcmingwen, 0, mingwen.length);
//初始化秘钥
SecretKey encryptionKey = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//AES、DES 在CBC 操作模式下需要iv 参数
IvParameterSpec iv = new IvParameterSpec(key.getBytes());
//加密
cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, iv);
byte[] result = cipher.doFinal(tcmingwen);
//LogUtils.e("yhj","加密密文是("+result.length+"):"+ToolUtils.bytes2HexString(result));
return result;
}
//解密
public static byte[] decryption(byte[] miwen, String key) {
if (key.length() != 16) {
LogUtils.eTag("TAG", "key is err");
return "keylength is error".getBytes();
}
//初始化秘钥
SecretKey decryptionKey = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM);
Cipher cipher = null;
try {
cipher = Cipher.getInstance(CIPHER_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
//AES、DES 在CBC 操作模式下需要iv 参数
IvParameterSpec iv = new IvParameterSpec(key.getBytes());
//解密
try {
cipher.init(Cipher.DECRYPT_MODE, decryptionKey, iv);
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
byte[] result = new byte[0];
try {
result = cipher.doFinal(miwen);
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
// //读取消息体长度,删除多余的0
// byte[] msgBodyAttributes = Arrays.copyOfRange(result, 2, 4);
// int BodyLength = BitOperator.msgHeaderGetBodyLength(msgBodyAttributes);
// byte[] realresult = Arrays.copyOfRange(result, 0, BodyLength+13);
//
// LogUtils.e("yhj","去0明文是("+realresult.length+"):"+bytesToHexString(realresult));
return result;
}
}