PHP code example of idlegg / blockcrypto_sjj1310

1. Go to this page and download the library: Download idlegg/blockcrypto_sjj1310 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

idlegg / blockcrypto_sjj1310 example snippets

 PHP

("vendor/autoload.php");
//mds;
use Bytes as Bytes;

$srvIp = "192.168.19.21";
$srvPort = 8018;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
var_dump($socket);
$result=socket_connect($socket,$srvIp,$srvPort);
if($result){
    echo "连接成功\n";
	$key = "RAD2F4FC1E7898465AD2F4FC1E7898465";
	//$key = 1;
            
    //明文数据
    $dataArray = array("Message1 to be Encrypted", "第二个待加密数据", "\x00\x01\x02\x03");
            
    $tmp1 = "一个测试程序。";
    echo "字符串长:".strlen($tmp1)."\n";
    $tmp2 = Bytes::getBytes($tmp1);
    echo "数组长:".count($tmp2)."\n";
            
           
    try{
		// 此方法为多块数据加密
		$rsp3 = cmds::SW_blocksEncrypt($socket, 0, 0x00A, $key, null, 0, null, 0, null, $dataArray);
		echo "rsp3 ArrayLength=".count($rsp3)."\n";
		for($i = 0; $i<count($rsp3); $i++){
			echo "\nCipher:".$i."\n";
			var_dump(Bytes::getBytes($rsp3[$i]));    //加密后数据密文转为字节数组查看
		}
    }catch(Exception $e){   //失败时抛出异常,如密码机报错则异常信息中包含错误码
        echo 'Encrypt Failed, Message: ' .$e->getMessage();
    }

    try{
        // 此方法为多块数据解密
		$rsp4 = cmds::SW_blocksDecrypt($socket, 0, 0x00A, $key, null, 0, null, 0, null, $rsp3);
		echo "rsp4 ArrayLength=".count($rsp4)."\n";
        for($i = 0; $i<count($rsp4); $i++){
            echo "\nPlain:".$i.$rsp4[$i]."\n";  //以字符串形式打印解密后的明文信息
            var_dump(Bytes::getBytes($rsp4[$i]));   //解密后数据密文转为字节数组查看
        }
    }catch(Exception $e){   //失败时抛出异常,如密码机报错则异常信息中包含错误码
        echo 'Decrypt Failed, Message: ' .$e->getMessage();
    }
}
socket_close($socket);
 PHP
public static function SW_blocksEncrypt(
    $socket,
    $encFlag,
    $keyTpe,
    $key,
    $deriveFactor,
    $sessionKeyFlag,
    $sessionKeyFactor,
    $paddingFlag,
    $iv,
    $dataArray);
 PHP
public static function SW_blocksDecrypt(
    $socket,
    $encFlag,
    $keyType,
    $key,
    $deriveFactor,
    $sessionKeyFlag,
    $sessionKeyFactor,
    $paddingFlag,
    $iv,
    $dataArray){