Skip to content

Commit e74cabe

Browse files
committed
修改执行错误时的处理方式,从退出程序改为抛出异常
1 parent 571f1d1 commit e74cabe

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

Bytes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public static function hexStringToBytes($hex) {
153153
}
154154
return $result;
155155
}
156-
156+
157157

158158
private static $hexStr = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
159159
public static function bytesToHexstring($bytesArray){

README.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,43 @@ and open the template in the editor.
2727
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
2828
var_dump($socket);
2929
$result=socket_connect($socket,$srvIp,$srvPort);
30+
3031
if($result){
31-
echo "连接成功<br />";
32+
echo "连接成功\n";
33+
$key = "RAD2F4FC1E7898465AD2F4FC1E7898465";
34+
//$key = 1;
3235

3336
//明文数据
3437
$dataArray = array("Message1 to be Encrypted", "第二个待加密数据", "\x00\x01\x02\x03");
3538

3639
$tmp1 = "一个测试程序。";
37-
echo "字符串长:".strlen($tmp1)."<br />";
40+
echo "字符串长:".strlen($tmp1)."\n";
3841
$tmp2 = Bytes::getBytes($tmp1);
39-
echo "数组长:".count($tmp2)."<br />";
42+
echo "数组长:".count($tmp2)."\n";
4043

41-
try{
4244

45+
try{
4346
// 此方法为多块数据加密
44-
$rsp3 = cmds::SW_blocksEncrypt($socket, 0, 0x00A, 1, null, 0, null, 0, null, $dataArray);
45-
for($i = 0; $i<count($dataArray); $i++){
46-
echo "<br />Cipher:".$i.$rsp3[$i]."<br />";
47+
$rsp3 = cmds::SW_blocksEncrypt($socket, 0, 0x00A, $key, null, 0, null, 0, null, $dataArray);
48+
echo "rsp3 ArrayLength=".count($rsp3)."\n";
49+
for($i = 0; $i<count($rsp3); $i++){
50+
echo "\nCipher:".$i.$rsp3[$i]."\n";
4751
var_dump(Bytes::getBytes($rsp3[$i])); //加密后数据密文转为字节数组查看
4852
}
53+
}catch(Exception $e){ //失败时抛出异常,如密码机报错则异常信息中包含错误码
54+
echo 'Encrypt Failed, Message: ' .$e->getMessage();
55+
}
4956

57+
try{
5058
// 此方法为多块数据解密
51-
$rsp4 = cmds::SW_blocksDecrypt($socket, 0, 0x00A, 1, null, 0, null, 0, null, $rsp3);
52-
for($i = 0; $i<count($dataArray); $i++){
53-
echo "<br />Plain:".$i.$rsp4[$i]."<br />"; //以字符串形式打印解密后的明文信息
59+
$rsp4 = cmds::SW_blocksDecrypt($socket, 0, 0x00A, $key, null, 0, null, 0, null, $rsp3);
60+
echo "rsp4 ArrayLength=".count($rsp4)."\n";
61+
for($i = 0; $i<count($rsp4); $i++){
62+
echo "\nPlain:".$i.$rsp4[$i]."\n"; //以字符串形式打印解密后的明文信息
5463
var_dump(Bytes::getBytes($rsp4[$i])); //解密后数据密文转为字节数组查看
5564
}
5665
}catch(Exception $e){ //失败时抛出异常,如密码机报错则异常信息中包含错误码
57-
echo 'Message: ' .$e->getMessage();
66+
echo 'Decrypt Failed, Message: ' .$e->getMessage();
5867
}
5968
}
6069
socket_close($socket);
@@ -69,7 +78,7 @@ and open the template in the editor.
6978
public static function SW_blocksEncrypt(
7079
$socket,
7180
$encFlag,
72-
$keyType,
81+
$keyTpe,
7382
$key,
7483
$deriveFactor,
7584
$sessionKeyFlag,

cmds.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ public static function SW_blocksEncrypt( $socket,
3434
$dataArray){
3535
$str = "SW0";
3636
if(is_int($encFlag) && $encFlag >= 0){ $str = $str.sprintf("%02X", $encFlag); }
37-
else{ die("Invalid argument \$encFlag=$encFlag"); }
37+
else{
38+
throw new Exception("Invalid argument \$encFlag=$encFlag");
39+
}
3840
if(is_int($keyType) & $keyType >= 0){ $str = $str.sprintf("%03X", $keyType); }
39-
else{ die("Invalid argument \$keyType=$keyType"); }
41+
else{ throw new Exception("Invalid argument \$keyType=$keyType"); }
4042
if(is_int($key) && $key > 0 && $key <= 9999){ $str = $str.sprintf("K%04d", $key); }
4143
else if(!is_null($key) && is_string($key) && (strlen($key) == 16 || (strlen($key)-1)%32 == 0)){ $str = $str.$key; }
42-
else{ die("Invalid argument \$key=$key"); }
44+
else{ throw new Exception("Invalid argument \$key=$key"); }
4345
if(!is_null($deriveFactor) && is_string($deriveFactor)){
4446
$str = $str.sprintf("%02X", strlen($deriveFactor)/32);
4547
$str = $str.$deriveFactor;
@@ -48,7 +50,7 @@ public static function SW_blocksEncrypt( $socket,
4850
$str = $str."00";
4951
}
5052
else{
51-
die("Invalid argument \$deriveFactor=$deriveFactor");
53+
throw new Exception("Invalid argument \$deriveFactor=$deriveFactor");
5254
}
5355
if(is_int($sessionKeyFlag) && $sessionKeyFlag >= 0){
5456
$str = $str.sprintf("%02X", $sessionKeyFlag);
@@ -60,7 +62,7 @@ public static function SW_blocksEncrypt( $socket,
6062
$str = $str.sprintf("%02X", $paddingFlag);
6163
}
6264
else {
63-
die("Invalid argument \$paddingFlag=$paddingFlag");
65+
throw new Exception("Invalid argument \$paddingFlag=$paddingFlag");
6466
}
6567

6668
if(!is_null($dataArray) && is_array($dataArray)){
@@ -79,13 +81,19 @@ public static function SW_blocksEncrypt( $socket,
7981
socket_write($socket, $str, strlen($str));
8082
$rsp=socket_read($socket,2);
8183
if($rsp == FALSE){
82-
die(socket_strerror(socket_last_error()));
84+
throw new Exception(socket_strerror(socket_last_error()));
8385
}
8486
$len = Bytes::bytesToShortBigEnd(Bytes::getBytes($rsp), 0);
8587
$rsp = socket_read($socket, $len);
8688
if($rsp == FALSE){
87-
die(socket_strerror(socket_last_error()));
89+
throw new Exception(socket_strerror(socket_last_error()));
8890
}
91+
92+
$rv = intval(substr($rsp, 2, 2));
93+
if($rv !== 0){
94+
throw new Exception("Failed with returnCode [".$rv."].\n");
95+
}
96+
8997

9098
$ret = array();
9199
$offset = 4;
@@ -190,6 +198,12 @@ public static function SW_blocksDecrypt( $socket,
190198
if($rsp == FALSE){
191199
throw new Exception(socket_strerror());
192200
}
201+
202+
$rv = intval(substr($rsp, 2, 2));
203+
if($rv !== 0){
204+
throw new Exception("Failed with returnCode [".$rv."].\n");
205+
}
206+
193207
$ret = array();
194208
$offset = 4;
195209
while($offset < $len){

0 commit comments

Comments
 (0)