Commit 0ed7dd8f authored by AfirSraftGarrier's avatar AfirSraftGarrier

格式并加些打印

parent 80ea7364
......@@ -5,10 +5,15 @@ import iot.sixiang.license.object.data.AuthData;
import iot.sixiang.license.util.HmacUtil;
import iot.sixiang.license.util.sm4.SM4;
import iot.sixiang.license.util.sm4.SM4Context;
import lombok.SneakyThrows;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Random;
/**
......@@ -25,15 +30,58 @@ public class DeviceControllerTest {
String appKey = "110801";
//byte[] baseKeyBytes = "nAOq38p4bGQyF4FG".getBytes();
//System.out.println(baseKeyBytes.length);
showSendData(appId, sn, appKey);
byte[] allBytes = getAuthSendBytes(appId, sn, appKey);
System.out.println(bytes2Hex(allBytes));
}
private void doString() {
@SneakyThrows
@Test
void socket() {
String serverIp = "112.74.57.111";
int serverPort = 18889;
Socket socket = new Socket(serverIp, serverPort);
System.out.println("connect success...");
// 事先组装好要发送的鉴权信息
String appId = "ebsh71dp5t1ck948l5";
String sn = "ERE54S619LNYMPKVN9";
String appKey = "110801";
byte[] authSendBytes = getAuthSendBytes(appId, sn, appKey);
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
// 鉴权
sendAndReceive(socket, outputStream, inputStream, "auth", authSendBytes);
// 鉴权后做动作
String request = "55AA340090000156F43D4D806827D1BB8F78C00D2B5488479CEB172C5D477C4D3023CE7B111621D4AD2C11ACAC4D876A3AC66A82F5F19000EF";
byte[] actionSendBytes = hex2Byte(request);
byte[] actionResultBytes = sendAndReceive(socket, outputStream, inputStream, "action", actionSendBytes);
String result = "55AA1500A40035414E7DB5B3EDE72BC6BA44203F52EA0000005B";
Assert.assertEquals(bytes2Hex(actionResultBytes), result);
inputStream.close();
outputStream.close();
socket.close();
}
@SneakyThrows
private byte[] sendAndReceive(Socket socket, OutputStream outputStream, InputStream inputStream, String tag, byte[] requestBytes) {
System.out.println(tag + " send:" + bytes2Hex(requestBytes));
outputStream.write(requestBytes);
byte[] bytes = new byte[1024];
int count = inputStream.read(bytes);
byte[] resultBytes = new byte[count];
for (int i = 0; i < count; i++) {
resultBytes[i] = bytes[i];
}
System.out.println(tag + " receive:" + bytes2Hex(resultBytes));
return requestBytes;
}
private void showSendData(String appId, String sn, String appKey) {
private byte[] getAuthSendBytes(String appId, String sn, String appKey) {
byte[] stxBytes = {(byte) 0x55, (byte) 0xaa};
// 这部分生成内容
......@@ -77,9 +125,7 @@ public class DeviceControllerTest {
endByte = (byte) (endByte ^ dataBytes[i]);
}
byte[] allBytes = byteMerger(stxBytes, lenBytes, new byte[]{cmdByte, ackByte}, dataBytes, new byte[]{endByte});
System.out.println(bytes2Hex(allBytes));
return byteMerger(stxBytes, lenBytes, new byte[]{cmdByte, ackByte}, dataBytes, new byte[]{endByte});
}
public byte[] encryptData_ECB(String string, byte[] keyBytes) {
......@@ -147,6 +193,25 @@ public class DeviceControllerTest {
return builder.toString();
}
public static byte[] hex2Byte(String string) {
if (string == null || string.length() < 1) {
return null;
}
// 因为一个byte生成两个字符,长度对应1:2,所以byte[]数组长度是字符串长度一半
byte[] bytes = new byte[string.length() / 2];
// 遍历byte[]数组,遍历次数是字符串长度一半
for (int i = 0; i < string.length() / 2; i++) {
// 截取没两个字符的前一个,将其转为int数值
int high = Integer.parseInt(string.substring(i * 2, i * 2 + 1), 16);
// 截取没两个字符的后一个,将其转为int数值
int low = Integer.parseInt(string.substring(i * 2 + 1, i * 2 + 2), 16);
// 高位字符对应的int值*16+低位的int值,强转成byte数值即可
// 如dd,高位13*16+低位13=221(强转成byte二进制11011101,对应十进制-35)
bytes[i] = (byte) (high * 16 + low);
}
return bytes;
}
//public static void main(String[] args) {
// byte[] stxbyte = {(byte) 0x7E};
// byte[] typebyte = {(byte) 0x01};
......@@ -201,30 +266,4 @@ public class DeviceControllerTest {
// sb.append("]");
// return sb.toString();
//}
///**
// * 将16进制字符转换成byte[]数组。与byte2Hex功能相反。
// *
// * @param string 16进制字符串
// * @return byte[]数组
// */
//public static byte[] hex2Byte(String string) {
// if (string == null || string.length() < 1) {
// return null;
// }
// // 因为一个byte生成两个字符,长度对应1:2,所以byte[]数组长度是字符串长度一半
// byte[] bytes = new byte[string.length() / 2];
// // 遍历byte[]数组,遍历次数是字符串长度一半
// for (int i = 0; i < string.length() / 2; i++) {
// // 截取没两个字符的前一个,将其转为int数值
// int high = Integer.parseInt(string.substring(i * 2, i * 2 + 1), 16);
// // 截取没两个字符的后一个,将其转为int数值
// int low = Integer.parseInt(string.substring(i * 2 + 1, i * 2 + 2), 16);
// // 高位字符对应的int值*16+低位的int值,强转成byte数值即可
// // 如dd,高位13*16+低位13=221(强转成byte二进制11011101,对应十进制-35)
// bytes[i] = (byte) (high * 16 + low);
// }
// return bytes;
//}
}
\ No newline at end of file
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