Commit d7049cf1 authored by AfirSraftGarrier's avatar AfirSraftGarrier

格式并加些打印

parent 8e6d8012
...@@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test; ...@@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.Random;
/** /**
* Created by M=54G * Created by M=54G
...@@ -22,27 +23,45 @@ public class DeviceControllerTest { ...@@ -22,27 +23,45 @@ public class DeviceControllerTest {
String appId = "ebsh71dp5t1ck948l5"; String appId = "ebsh71dp5t1ck948l5";
String sn = "ERE54S619LNYMPKVN9"; String sn = "ERE54S619LNYMPKVN9";
String appKey = "110801"; String appKey = "110801";
//showSendData(appId, sn, appKey); //byte[] baseKeyBytes = "nAOq38p4bGQyF4FG".getBytes();
//System.out.println(baseKeyBytes.length);
showSendData(appId, sn, appKey);
} }
private void showSendData(String appId, String sn, String appKey) { private void showSendData(String appId, String sn, String appKey) {
byte[] stxBytes = {(byte) 0x55, (byte) 0xaa}; byte[] stxBytes = {(byte) 0x55, (byte) 0xaa};
// 这部分生成内容
// 用于SM4
byte[] randomBytes = new byte[16];
Random random = new Random();
for (int i = 0; i < randomBytes.length; i++) {
randomBytes[i] = (byte) (random.nextInt());
}
byte[] sm4KeyBytes = new byte[16];
byte[] baseKeyBytes = "nAOq38p4bGQyF4FG".getBytes();
for (int i = 0; i < randomBytes.length; i++) {
sm4KeyBytes[i] = (byte) (randomBytes[i] ^ baseKeyBytes[i]);
}
String string = "app_id=" + appId + "&sn=" + sn; String string = "app_id=" + appId + "&sn=" + sn;
String sign = HmacUtil.encrypt(string, appKey, HmacUtil.HMAC_SHA1); String sign = HmacUtil.encrypt(string, appKey, HmacUtil.HMAC_SHA1);
// 组装
AuthData authData = new AuthData(); AuthData authData = new AuthData();
authData.setAppId(appId); authData.setAppId(appId);
authData.setSn(sn); authData.setSn(sn);
authData.setSign(sign); authData.setSign(sign);
byte[] dataBytes = JSON.toJSONString(authData).getBytes(); byte[] dataBytes = encryptData_ECB(JSON.toJSONString(authData), sm4KeyBytes);
dataBytes = byteMerger(randomBytes, dataBytes);
int length = dataBytes.length + stxBytes.length; int length = dataBytes.length + stxBytes.length;
byte[] lenBytes = {(byte) (0xff & length), (byte) (0xff & (length >> 8))}; byte[] lenBytes = {(byte) length, (byte) (length >> 8)};
// 这部分是命令
byte cmdByte = (byte) 0x01; byte cmdByte = (byte) 0x01;
byte ackByte = (byte) 0x00; byte ackByte = (byte) 0x00;
// 结束符
byte endByte = (byte) 0x00; byte endByte = (byte) 0x00;
// 加密最后字段 // 加密最后字段
...@@ -117,7 +136,7 @@ public class DeviceControllerTest { ...@@ -117,7 +136,7 @@ public class DeviceControllerTest {
byte[] dataBytes = JSON.toJSONString(authData).getBytes(); byte[] dataBytes = JSON.toJSONString(authData).getBytes();
int length = dataBytes.length + stxBytes.length; int length = dataBytes.length + stxBytes.length;
byte[] lenBytes = {(byte) (0xff & length), (byte) (0xff & (length >> 8))}; byte[] lenBytes = {(byte) length, (byte) (length >> 8)};
byte cmdByte = (byte) 0x01; byte cmdByte = (byte) 0x01;
byte ackByte = (byte) 0x00; byte ackByte = (byte) 0x00;
...@@ -187,7 +206,7 @@ public class DeviceControllerTest { ...@@ -187,7 +206,7 @@ public class DeviceControllerTest {
byte[] baKeyword = new byte[s.length() / 2]; byte[] baKeyword = new byte[s.length() / 2];
for (int i = 0; i < baKeyword.length; i++) { for (int i = 0; i < baKeyword.length; i++) {
try { try {
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16)); baKeyword[i] = (byte) Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment