ParamUtil.java
7.73 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
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
package com.glsx.libble.util;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.DeviceUtils;
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.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
/**
* @Package: com.glsx.ble_module.util
* @ClassName: ParamUtil
* @Company GLSX
* @Email yangks@didihu.com.cn
* @Description: 作用描述:车钥匙蓝牙通信参数工具类
* @Author: yangks
* @CreateDate: 2021/4/15 11:35
* @Version:
*/
public class ParamUtil {
private final static String CREP = "125ACDF4554BE351";
private final static byte[] STRUCT_HEAD = {0x7B};//结构体头部
private final static byte[] STRUCT_TAIL = {0x7D};//结构体尾部
/**
* 获取手机MAC地址,不够8字节,末尾补00
*
* @return
*/
public static byte[] getPhoneMac() {
String mac = DeviceUtils.getMacAddress();
String[] macBytes = mac.split(":");
StringBuilder stringBuilder = new StringBuilder();
for (String b : macBytes) {
stringBuilder.append(b);
}
stringBuilder.append("0000");
return hexStringToBytes(stringBuilder.toString());
}
/**
* 获取手机MAC地址,6字节,带冒号
*
* @return
*/
public static String getPhoneMac6() {
return DeviceUtils.getMacAddress().toUpperCase();
}
/**
* 十六进制字符串转十六进制
*
* @param hexString
* @return
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null) {
return new byte[]{};
}
return ConvertUtils.hexString2Bytes(hexString);
}
/**
* 获取参数
* 参数结构十六进制 structHead(标识头--1个字节) + msgHead(消息头--一个字节) + bodyHead(消息体头部--8个字节)
* + bodyTail(消息体尾部--8个字节) + msgTail(检验码--1个字节) + structTail(识别尾--1个字节)
* 例如:7B 30 E01F88AB9BFF0000 125ACDF4554BE351 2E 7D
*
* @param msgHead
* @param bodyHead
* @param bodyTail
* @param key 加密随机字符串 注册为固定:125ACDF4554BE352,鉴权:随机生成
* @return
*/
public static byte[] getParam(byte[] msgHead, byte[] bodyHead, byte[] bodyTail, String key) {
// LogUtils.d("---消息头--一个字节---" + ToolUtils.bytes2HexString(msgHead));
// LogUtils.d("---消息体头部--8个字节---" + ToolUtils.bytes2HexString(bodyHead));
// LogUtils.d("---消息体尾部--8个字节---" + ToolUtils.bytes2HexString(bodyTail));
// LogUtils.d("---加密key---" + key);
byte[] body = addBytes(bodyHead, bodyTail);//拼接消息体
byte[] enBody = new byte[0];//加密消息体
try {
enBody = AESEncryptUtils.encryption(body, key);
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
//LogUtils.d("---"+enBody.length);
// LogUtils.d("---加密后消息体---" + ToolUtils.bytes2HexString(enBody));
byte[] bytes = addBytes(addBytes(STRUCT_HEAD, msgHead), enBody);
// LogUtils.d("---表示头+消息头+加密后消息体---" + ToolUtils.bytes2HexString(bytes));
byte[] checkBytes = getCheckCode(addBytes(msgHead, enBody));
// LogUtils.d("---校验码---" + ToolUtils.bytes2HexString(checkBytes));
byte[] resBytes = addBytes(addBytes(bytes, checkBytes), STRUCT_TAIL);
LogUtils.d("---完整体---" + ConvertUtils.bytes2HexString(resBytes));
//LogUtils.d("---"+ToolUtils.bytes2HexString(resBytes));
// LogUtils.d("--------++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-" + resBytes.length);
return resBytes;
}
/**
* 获取消息体----body
*
* @param data 完整通信协议20字节
* @return body
*/
public static byte[] getBody(byte[] data) {
if (data.length != 20) {
throw new IllegalArgumentException("数据长度不等于20字节");
}
byte[] resBytes = new byte[16];
int j = 0;
for (int i = 2; i < 18; i++) {
resBytes[j] = data[i];
j++;
}
return resBytes;
}
/**
* 获取消息体前8个字节----bodyHead
*
* @param data 完整消息体16字节
* @return bodyHead
*/
public static byte[] getBodyHead(byte[] data) {
if (data.length != 16) {
throw new IllegalArgumentException("数据长度不等于16字节");
}
byte[] resBytes = new byte[8];
int j = 0;
for (int i = 0; i < 8; i++) {
resBytes[j] = data[i];
j++;
}
return resBytes;
}
/**
* 获取消息体后8个字节----bodyTail
*
* @param data 完整消息体16字节
* @return bodyTail
*/
public static byte[] getBodyTail(byte[] data) {
if (data.length != 16) {
throw new IllegalArgumentException("数据长度不等于16字节");
}
byte[] resBytes = new byte[8];
int j = 0;
for (int i = 8; i < 16; i++) {
resBytes[j] = data[i];
j++;
}
return resBytes;
}
/**
* 生成校验码
*
* @param bytes
* @return
*/
public static byte[] getCheckCode(byte[] bytes) {
byte res = 0x00;
for (byte b : bytes) {
res = (byte) (b ^ res);
}
byte[] resBytes = {res};
return resBytes;
}
/**
* 拼接两个byte数组
*
* @param data1
* @param data2
* @return data1 与 data2拼接的结果
*/
public static byte[] addBytes(byte[] data1, byte[] data2) {
byte[] data3 = new byte[data1.length + data2.length];
System.arraycopy(data1, 0, data3, 0, data1.length);
System.arraycopy(data2, 0, data3, data1.length, data2.length);
return data3;
}
/**
* 比较两个字节数组
*
* @param data1
* @param data2
* @return
*/
public static boolean compareBytes(byte[] data1, byte[] data2) {
if (data1.length != data2.length) {
return false;
}
int i = 0;
while (i < data1.length) {
if (data1[i] != data2[i]) {
return false;
}
i++;
}
return true;
}
/**
* 生成8字节 16进制随机byte数组
*
* @return
*/
public static byte[] getRandom() {
return ConvertUtils.hexString2Bytes(getRandomValue());
}
/*产生8位16进制的数*/
public static String getRandomValue() {
String str = "";
for (int i = 0; i < 16; i++) {
char temp = 0;
int key = (int) (Math.random() * 2);
switch (key) {
case 0:
temp = (char) (Math.random() * 10 + 48);//产生随机数字
break;
case 1:
temp = (char) (Math.random() * 6 + 'a');//产生a-f
break;
default:
break;
}
str = str + temp;
}
return str;
}
}