Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
ioc_sixiang_license
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zengtianlai3
ioc_sixiang_license
Commits
0ed7dd8f
Commit
0ed7dd8f
authored
Nov 15, 2022
by
AfirSraftGarrier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
格式并加些打印
parent
80ea7364
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
71 additions
and
32 deletions
+71
-32
DeviceControllerTest.java
.../iot/sixiang/license/controller/DeviceControllerTest.java
+71
-32
No files found.
license/src/test/java/iot/sixiang/license/controller/DeviceControllerTest.java
View file @
0ed7dd8f
...
@@ -5,10 +5,15 @@ import iot.sixiang.license.object.data.AuthData;
...
@@ -5,10 +5,15 @@ import iot.sixiang.license.object.data.AuthData;
import
iot.sixiang.license.util.HmacUtil
;
import
iot.sixiang.license.util.HmacUtil
;
import
iot.sixiang.license.util.sm4.SM4
;
import
iot.sixiang.license.util.sm4.SM4
;
import
iot.sixiang.license.util.sm4.SM4Context
;
import
iot.sixiang.license.util.sm4.SM4Context
;
import
lombok.SneakyThrows
;
import
org.junit.Assert
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.net.Socket
;
import
java.util.Random
;
import
java.util.Random
;
/**
/**
...
@@ -25,15 +30,58 @@ public class DeviceControllerTest {
...
@@ -25,15 +30,58 @@ public class DeviceControllerTest {
String
appKey
=
"110801"
;
String
appKey
=
"110801"
;
//byte[] baseKeyBytes = "nAOq38p4bGQyF4FG".getBytes();
//byte[] baseKeyBytes = "nAOq38p4bGQyF4FG".getBytes();
//System.out.println(baseKeyBytes.length);
//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"
;
String
request
=
"55AA340090000156F43D4D806827D1BB8F78C00D2B5488479CEB172C5D477C4D3023CE7B111621D4AD2C11ACAC4D876A3AC66A82F5F19000EF"
;
byte
[]
actionSendBytes
=
hex2Byte
(
request
);
byte
[]
actionResultBytes
=
sendAndReceive
(
socket
,
outputStream
,
inputStream
,
"action"
,
actionSendBytes
);
String
result
=
"55AA1500A40035414E7DB5B3EDE72BC6BA44203F52EA0000005B"
;
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
};
byte
[]
stxBytes
=
{(
byte
)
0x55
,
(
byte
)
0xaa
};
// 这部分生成内容
// 这部分生成内容
...
@@ -77,9 +125,7 @@ public class DeviceControllerTest {
...
@@ -77,9 +125,7 @@ public class DeviceControllerTest {
endByte
=
(
byte
)
(
endByte
^
dataBytes
[
i
]);
endByte
=
(
byte
)
(
endByte
^
dataBytes
[
i
]);
}
}
byte
[]
allBytes
=
byteMerger
(
stxBytes
,
lenBytes
,
new
byte
[]{
cmdByte
,
ackByte
},
dataBytes
,
new
byte
[]{
endByte
});
return
byteMerger
(
stxBytes
,
lenBytes
,
new
byte
[]{
cmdByte
,
ackByte
},
dataBytes
,
new
byte
[]{
endByte
});
System
.
out
.
println
(
bytes2Hex
(
allBytes
));
}
}
public
byte
[]
encryptData_ECB
(
String
string
,
byte
[]
keyBytes
)
{
public
byte
[]
encryptData_ECB
(
String
string
,
byte
[]
keyBytes
)
{
...
@@ -147,6 +193,25 @@ public class DeviceControllerTest {
...
@@ -147,6 +193,25 @@ public class DeviceControllerTest {
return
builder
.
toString
();
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) {
//public static void main(String[] args) {
// byte[] stxbyte = {(byte) 0x7E};
// byte[] stxbyte = {(byte) 0x7E};
// byte[] typebyte = {(byte) 0x01};
// byte[] typebyte = {(byte) 0x01};
...
@@ -201,30 +266,4 @@ public class DeviceControllerTest {
...
@@ -201,30 +266,4 @@ public class DeviceControllerTest {
// sb.append("]");
// sb.append("]");
// return sb.toString();
// 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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment