123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?php
- function string2hex($string){
- $hex = '';
- for($i = 0;$i < strlen($string); $i++){
- $ch = dechex(ord($string[$i]));
- if(strlen($ch)==1){
- $ch = "0".$ch;
- }
- $hex .=$ch;
- }
- return $hex;
- }
- /*
- function hex2string($hex){
- //echo "$hex \n";
- $string = '';
- for($i = 0;$i < strlen($hex);$i++){
- $string .= chr(hexdec($hex[$i].$hex[$i++]));
- }
- return $string;
- }
- */
- <<<<<<< HEAD
-
- =======
- >>>>>>> 386b37d33e5ba817cba00df29efaefbd692e4dd1
- if(!extension_loaded('phpcrypto')) {
- dl('phpcrypto.so');
- }
- $module = 'phpcrypto';
- $functions = get_extension_funcs($module);
- echo "Functions available in the test extension:\n";
- foreach($functions as $func) {
- echo $func."\n";
- }
- echo "\n";
- //先加载动态库,否则无法调用接口
- function test_php_crypto_init(){
- $php_func = 'php_crypto_init';
- echo "--------$php_func--------\n";
- <<<<<<< HEAD
- $path = "/www/wwwroot/hgd.liuniu946.com/public/opt/php/libcryptAPIsm_lnx64.so";//国密动态库路径
- =======
- $path = "/opt/php/libcryptAPIsm_lnx64.so";//国密动态库路径
- >>>>>>> 386b37d33e5ba817cba00df29efaefbd692e4dd1
- $recode = $php_func($path);
- echo "recode:$recode \n";//如果非零,则加载失败
- }
- //test php_SM4Crypt1
- function test_php_SM4Crypt1(){
- $php_func = 'php_SM4Crypt1';
- echo "--------$php_func--------\n";
- $data = "this is a test! 这是个测试!";
- $key = "1234567890123456";//如果是hex编码,务必解码
- $recode = $php_func(0,$data,$redata,$key);
- echo "recode:$recode \n";
- echo "redata: ".string2hex($redata)." len:".strlen($redata)." \n";
- $recode = $php_func(1,$redata,$dedata,$key);
- echo "recode:$recode \n";
- echo "plain: ".$dedata." \n";
- }
- //test SM4Crypt2
- function test_php_SM4Crypt2(){
- $php_func = 'php_SM4Crypt2';
- echo "--------$php_func--------\n";
- $data = "this is a test! 这是个测试!";
- $key = "1234567890123456";//如果是hex编码,务必解码
- $recode = $php_func(0,$data,$redata,$key);
- echo "recode:$recode \n";
- echo "redata: ".string2hex($redata)." len:".strlen($redata)." \n";
- $recode = $php_func(1,$redata,$dedata,$key);
- echo "recode:$recode \n";
- echo "plain: ".$dedata." \n";
- }
- //test php_CryptFile
- function test_php_CryptFile(){
- $php_func = 'php_CryptFile';
- echo "--------$php_func--------\n";
- $flag = 2;
- $infilename = "/home/essc50mysql/mf/src_crypt/test_cryptfile";
- $encodefilename = "/home/essc50mysql/mf/src_crypt/test_encode_cryptfile";
- $format = 1;
- $key = "1234567890123456";//如果是hex编码,务必解码
- $recode = $php_func($flag, $key, $infilename,$encodefilename,$format);
- echo "recode:$recode \n";
- $flag = 3;
- $decodefilename = "/home/essc50mysql/mf/src_crypt/test_decode_cryptfile";
- $recode = $php_func($flag, $key, $encodefilename,$decodefilename,$format);
- echo "recode:$recode \n";
- }
- //test php_CryptKey
- function test_php_CryptKey(){
- $php_func = 'php_CryptKey';
- echo "--------$php_func--------\n";
- $flag = 0;
- $plainkey = "1234567890123456";
- $recode = $php_func($flag, $plainkey,$cipherkey);
- echo "recode:$recode \n";
- echo "cipherkey: ".string2hex($cipherkey)." len:".strlen($cipherkey)." \n";
- $flag = 2;
- $recode = $php_func($flag, NULL,$genkey);
- echo "recode:$recode \n";
- echo "genkey: ".string2hex($genkey)." len:".strlen($genkey)." \n";
- }
- //test php_SM3Crypt
- function test_php_SM3Crypt(){
- $php_func = 'php_SM3Crypt';
- echo "--------$php_func--------\n";
- $data = "this is test 这是测试!";
- $recode = $php_func($data,$sm3hash);
- echo "recode:$recode \n";
- echo "sm3hash=================: ".string2hex($sm3hash)." len:".strlen($sm3hash)."============= \n";
- }
- //test php_MACCrypt
- function test_php_MACCrypt(){
- $php_func = 'php_MACCrypt';
- echo "--------$php_func--------\n";
- $flag = 0;
- $data = "this is test 这是测试!this is test 这是测试!";
- $key = "1234567890123456";
- $recode = $php_func($flag,$data, $key,$redata);
- echo "recode:$recode \n";
- echo "flag:".$flag." redata: ".string2hex($redata)." len:".strlen($redata)." \n";
- $flag = 1;
- $recode = $php_func($flag,$data, $key,$redata);
- echo "recode:$recode \n";
- echo "flag:".$flag." redata: ".string2hex($redata)." len:".strlen($redata)." \n";
- }
- //test php_CryptLmkMac
- function test_php_CryptLmkMac(){
- $php_func = 'php_CryptLmkMac';
- echo "--------$php_func--------\n";
- $recode = $php_func($data,$version, $pubkey);
- echo "recode:$recode \n";
- echo "keymac: ".string2hex($data)." version:".$version." pubkey".string2hex($pubkey)." len:".strlen($pubkey)." \n";
- }
- //test php_SM2Genkey
- function test_php_SM2Genkey(){
- $php_func = 'php_SM2Genkey';
- //echo "--------$php_func--------\n";
- $recode = $php_func($privkey, $pubkey);
- //echo "recode:$recode \n";
- echo "privkey: ".string2hex($privkey)." len:".strlen($privkey)." pubkey: ".string2hex($pubkey)." len:".strlen($pubkey)." \n";
- echo "sm2密钥对:"+array($privkey, $pubkey);
- return array($privkey, $pubkey);
- }
- //test php_SM2Sign
- function test_php_SM2SignAndSM2Verify(){
- $php_func = 'php_SM2Sign';
- echo "--------$php_func--------\n";
- $data = "this is test 这是测试!this is test 这是测试!";//数据有中文则需要注意字符编码的问题
- $sm2keys = test_php_SM2Genkey();//0 privkey 1 pubkey
- //签名
- $recode = $php_func($data, $redata,$sm2keys[0],$sm2keys[1]);
- echo "recode:$recode \n";
- echo "signValue: ".string2hex($redata)." len:".strlen($redata)." \n";
- //php_SM2Sign接口返回的签名值是RS格式,以下示例转DER编码
- $php_func = 'php_SM2FormatConvert';
- $recode = $php_func(202,$redata, $der);
- echo "recode:$recode \n";
- echo "signValue DER: ".string2hex($der)." len:".strlen($der)." \n";
- //验签
- $php_func = 'php_SM2Verify';
- $recode = $php_func($data, $redata,$sm2keys[1]);
- echo "recode:$recode \n";
- //php_SM2Verify 接收的签名值是RS格式,若对方给的签名值是DER格式
- //则通过如下方式转换,然后再验签
- $php_func = 'php_SM2FormatConvert';
- $recode = $php_func(201,$der, $rs);
- echo "recode:$recode \n";
- echo "signValue RS: ".string2hex($rs)." len:".strlen($rs)." \n";
- }
- //test php_SM2SignHash php_SM2VerifyHash
- function test_php_SM2SignHashAndSM2VerifyHash(){
- $php_func = 'php_SM2SignHash';
- echo "--------$php_func--------\n";
- $data = "this is test 这是测试!this is test 这是测试!";
- $sm2keys = test_php_SM2Genkey();//0 privkey 1 pubkey
- $php_func = 'php_SM3Crypt';
- $recode = $php_func($data,$sm3hash);
- echo "recode:$recode \n";
- echo "sm3hash: ".string2hex($sm3hash)." len:".strlen($sm3hash)." \n";
- $php_func = 'php_SM2SignHash';
- $recode = $php_func($sm3hash, $redata,$sm2keys[0]);
- echo "recode:$recode \n";
- echo "signValue: ".string2hex($redata)." len:".strlen($redata)." \n";
- $php_func = 'php_SM2VerifyHash';
- $recode = $php_func($sm3hash, $redata,$sm2keys[1]);
- echo "recode:$recode \n";
- }
- //test php_SM2Encrypt php_SM2Decrypt
- function test_php_SM2EncryptAndSM2Decrypt(){
- $php_func = 'php_SM2Encrypt';
- echo "--------$php_func--------\n";
- $data = "this is test 这是测试!this is test 这是测试!";
- $sm2keys = test_php_SM2Genkey();//0 privkey 1 pubkey
- //加密
- $recode = $php_func($data, $cipher,$sm2keys[1]);
- echo "recode:$recode \n";
- echo "cipher: ".string2hex($cipher)." len:".strlen($cipher)." \n";
- //php_SM2Encrypt接口返回的密文值是c1c3c2格式,以下示例转DER编码
- $php_func = 'php_SM2FormatConvert';
- $recode = $php_func(102,$cipher, $der);
- echo "recode:$recode \n";
- echo "cipher DER: ".string2hex($der)." len:".strlen($der)." \n";
- //解密
- $php_func = 'php_SM2Decrypt';
- $recode = $php_func($cipher, $plain,$sm2keys[0]);
- echo "recode:$recode \n";
- echo "plain: $plain\n";
- //php_SM2Decrypt 接收密文值是c1c3c2格式,若对方给的密文值是DER格式
- //则通过如下方式转换,然后再传入接口解密
- $php_func = 'php_SM2FormatConvert';
- $recode = $php_func(101,$der, $c1c3c2);
- echo "recode:$recode \n";
- echo "cipher c1c3c2: ".string2hex($c1c3c2)." len:".strlen($c1c3c2)." \n";
- }
- //test php_HextoAsc php_AsctoHex
- function test_php_HextoAscAndAsctoHex(){
- $php_func = 'php_AsctoHex';
- echo "--------$php_func--------\n";
- $data = "this is test 这是测试!this is test 这是测试!";
- $recode = $php_func($data, $hex);
- echo "recode:$recode \n";
- echo "hex: $hex len:".strlen($hex)." \n";
- $php_func = 'php_HextoAsc';
- $recode = $php_func($hex, $asc);
- echo "recode:$recode \n";
- echo "plain: $asc\n";
- }
- //test php_base64_decode php_base64_encode
- function test_php_base64_decodeAndbase64_encode(){
- $php_func = 'php_base64_encode';
- echo "--------$php_func--------\n";
- $data = "this is test 这是测试!this is test 这是测试!";
- $recode = $php_func($data, $base64);
- echo "recode:$recode \n";
- echo "base64char: $base64 len:".strlen($base64)." \n";
- $php_func = 'php_base64_decode';
- $recode = $php_func($base64, $orgin);
- echo "recode:$recode \n";
- echo "plain: $orgin\n";
- }
- //test
- function test_php_SM2FormatConvert(){
- $php_func = 'php_SM2FormatConvert';
- echo "--------$php_func--------\n";
- //hex编码的DER密文
- $data =
- "308198021F5A9D5395EE7A52463E07727CAEA3001A3D95ADF105992B3F8430C0B63D5272022011BC87B586EA976FF2A8009393FE3F71FEBD11FD249F59796054DB66B4789C6D04201D3FAEF883C8D839DDF36FC083B125DE65D8E58B3AACEB97CEBCF529A8F0C2A7043119C9E04B8DB90E6CC0898A85665DA1FA990B4DB6197AE55674D68C3AB5328D408D0B7EBAE99ED6C8243FED2A18C024545A";
- //先解码再转der格式(如果是base64编码,则用base64解码)
- $php_HextoAsc = 'php_HextoAsc';
- $recode = $php_HextoAsc($data, $dataAsc);
- $recode = $php_func(101,$dataAsc, $c1c3c2);
- echo "recode:$recode \n";
- echo "c1c3c2: ".string2hex($c1c3c2)." len:".strlen($c1c3c2)." \n";
- }
- //test SM4CBCCrypt
- function test_php_SM4CBCCrypt(){
- $php_func = 'php_SM4CBCCrypt';
- echo "--------$php_func--------\n";
- $data = "this is a test! 这是个测试!";
- $plainkey = "1234567890123456";//如果是hex编码,务必解码
- //接口是密文key,将明文key加密后再传入php_SM4CBCCrypt进行数据加密
- //不必要每次都加密key,建议手工加密一次,将key密文保存使用,key明文保存至安全的地方
- //若是会话级别的key则自行定策略
- $php_CryptKey = 'php_CryptKey';
- $recode = $php_CryptKey(0, $plainkey,$key);
- echo "recode:$recode \n";
- //打印key密文
- $php_hex = 'php_AsctoHex';
- $recode = $php_hex($key, $hex);
- echo "recode:$recode \n";
- echo "hex: $hex len:".strlen($hex)." \n";
- $iv = "1234567812345678";
- //加密
- $recode = $php_func(0,$data,$redata,$key,$iv);
- echo "recode:$recode \n";
- echo "redata: ".string2hex($redata)." len:".strlen($redata)." \n";
- //解密
- $recode = $php_func(1,$redata,$dedata,$key,$iv);
- echo "recode:$recode \n";
- echo "plain: ".$dedata." \n";
- }
- function run_test(){
- //先初始化接口,初始化成功后,可重复调用加解密接口
- test_php_crypto_init();
- test_php_SM4Crypt1();
- echo "\n";
- test_php_SM4Crypt2();
- echo "\n";
- test_php_CryptFile();
- echo "\n";
- test_php_CryptKey();
- echo "\n";
- test_php_SM3Crypt();
- echo "\n";
- test_php_MACCrypt();
- echo "\n";
- test_php_CryptLmkMac();
- echo "\n";
- test_php_SM2SignAndSM2Verify();
- echo "\n";
- test_php_SM2SignHashAndSM2VerifyHash();
- echo "\n";
- test_php_SM2EncryptAndSM2Decrypt();
- echo "\n";
- test_php_HextoAscAndAsctoHex();
- echo "\n";
- test_php_base64_decodeAndbase64_encode();
- echo "\n";
- test_php_SM2FormatConvert();
- echo "\n";
- test_php_SM4CBCCrypt();
- }
- run_test();
- ?>
|