phpcrypto.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. function string2hex($string){
  3. $hex = '';
  4. for($i = 0;$i < strlen($string); $i++){
  5. $ch = dechex(ord($string[$i]));
  6. if(strlen($ch)==1){
  7. $ch = "0".$ch;
  8. }
  9. $hex .=$ch;
  10. }
  11. return $hex;
  12. }
  13. /*
  14. function hex2string($hex){
  15. //echo "$hex \n";
  16. $string = '';
  17. for($i = 0;$i < strlen($hex);$i++){
  18. $string .= chr(hexdec($hex[$i].$hex[$i++]));
  19. }
  20. return $string;
  21. }
  22. */
  23. <<<<<<< HEAD
  24. =======
  25. >>>>>>> 386b37d33e5ba817cba00df29efaefbd692e4dd1
  26. if(!extension_loaded('phpcrypto')) {
  27. dl('phpcrypto.so');
  28. }
  29. $module = 'phpcrypto';
  30. $functions = get_extension_funcs($module);
  31. echo "Functions available in the test extension:\n";
  32. foreach($functions as $func) {
  33. echo $func."\n";
  34. }
  35. echo "\n";
  36. //先加载动态库,否则无法调用接口
  37. function test_php_crypto_init(){
  38. $php_func = 'php_crypto_init';
  39. echo "--------$php_func--------\n";
  40. <<<<<<< HEAD
  41. $path = "/www/wwwroot/hgd.liuniu946.com/public/opt/php/libcryptAPIsm_lnx64.so";//国密动态库路径
  42. =======
  43. $path = "/opt/php/libcryptAPIsm_lnx64.so";//国密动态库路径
  44. >>>>>>> 386b37d33e5ba817cba00df29efaefbd692e4dd1
  45. $recode = $php_func($path);
  46. echo "recode:$recode \n";//如果非零,则加载失败
  47. }
  48. //test php_SM4Crypt1
  49. function test_php_SM4Crypt1(){
  50. $php_func = 'php_SM4Crypt1';
  51. echo "--------$php_func--------\n";
  52. $data = "this is a test! 这是个测试!";
  53. $key = "1234567890123456";//如果是hex编码,务必解码
  54. $recode = $php_func(0,$data,$redata,$key);
  55. echo "recode:$recode \n";
  56. echo "redata: ".string2hex($redata)." len:".strlen($redata)." \n";
  57. $recode = $php_func(1,$redata,$dedata,$key);
  58. echo "recode:$recode \n";
  59. echo "plain: ".$dedata." \n";
  60. }
  61. //test SM4Crypt2
  62. function test_php_SM4Crypt2(){
  63. $php_func = 'php_SM4Crypt2';
  64. echo "--------$php_func--------\n";
  65. $data = "this is a test! 这是个测试!";
  66. $key = "1234567890123456";//如果是hex编码,务必解码
  67. $recode = $php_func(0,$data,$redata,$key);
  68. echo "recode:$recode \n";
  69. echo "redata: ".string2hex($redata)." len:".strlen($redata)." \n";
  70. $recode = $php_func(1,$redata,$dedata,$key);
  71. echo "recode:$recode \n";
  72. echo "plain: ".$dedata." \n";
  73. }
  74. //test php_CryptFile
  75. function test_php_CryptFile(){
  76. $php_func = 'php_CryptFile';
  77. echo "--------$php_func--------\n";
  78. $flag = 2;
  79. $infilename = "/home/essc50mysql/mf/src_crypt/test_cryptfile";
  80. $encodefilename = "/home/essc50mysql/mf/src_crypt/test_encode_cryptfile";
  81. $format = 1;
  82. $key = "1234567890123456";//如果是hex编码,务必解码
  83. $recode = $php_func($flag, $key, $infilename,$encodefilename,$format);
  84. echo "recode:$recode \n";
  85. $flag = 3;
  86. $decodefilename = "/home/essc50mysql/mf/src_crypt/test_decode_cryptfile";
  87. $recode = $php_func($flag, $key, $encodefilename,$decodefilename,$format);
  88. echo "recode:$recode \n";
  89. }
  90. //test php_CryptKey
  91. function test_php_CryptKey(){
  92. $php_func = 'php_CryptKey';
  93. echo "--------$php_func--------\n";
  94. $flag = 0;
  95. $plainkey = "1234567890123456";
  96. $recode = $php_func($flag, $plainkey,$cipherkey);
  97. echo "recode:$recode \n";
  98. echo "cipherkey: ".string2hex($cipherkey)." len:".strlen($cipherkey)." \n";
  99. $flag = 2;
  100. $recode = $php_func($flag, NULL,$genkey);
  101. echo "recode:$recode \n";
  102. echo "genkey: ".string2hex($genkey)." len:".strlen($genkey)." \n";
  103. }
  104. //test php_SM3Crypt
  105. function test_php_SM3Crypt(){
  106. $php_func = 'php_SM3Crypt';
  107. echo "--------$php_func--------\n";
  108. $data = "this is test 这是测试!";
  109. $recode = $php_func($data,$sm3hash);
  110. echo "recode:$recode \n";
  111. echo "sm3hash=================: ".string2hex($sm3hash)." len:".strlen($sm3hash)."============= \n";
  112. }
  113. //test php_MACCrypt
  114. function test_php_MACCrypt(){
  115. $php_func = 'php_MACCrypt';
  116. echo "--------$php_func--------\n";
  117. $flag = 0;
  118. $data = "this is test 这是测试!this is test 这是测试!";
  119. $key = "1234567890123456";
  120. $recode = $php_func($flag,$data, $key,$redata);
  121. echo "recode:$recode \n";
  122. echo "flag:".$flag." redata: ".string2hex($redata)." len:".strlen($redata)." \n";
  123. $flag = 1;
  124. $recode = $php_func($flag,$data, $key,$redata);
  125. echo "recode:$recode \n";
  126. echo "flag:".$flag." redata: ".string2hex($redata)." len:".strlen($redata)." \n";
  127. }
  128. //test php_CryptLmkMac
  129. function test_php_CryptLmkMac(){
  130. $php_func = 'php_CryptLmkMac';
  131. echo "--------$php_func--------\n";
  132. $recode = $php_func($data,$version, $pubkey);
  133. echo "recode:$recode \n";
  134. echo "keymac: ".string2hex($data)." version:".$version." pubkey".string2hex($pubkey)." len:".strlen($pubkey)." \n";
  135. }
  136. //test php_SM2Genkey
  137. function test_php_SM2Genkey(){
  138. $php_func = 'php_SM2Genkey';
  139. //echo "--------$php_func--------\n";
  140. $recode = $php_func($privkey, $pubkey);
  141. //echo "recode:$recode \n";
  142. echo "privkey: ".string2hex($privkey)." len:".strlen($privkey)." pubkey: ".string2hex($pubkey)." len:".strlen($pubkey)." \n";
  143. echo "sm2密钥对:"+array($privkey, $pubkey);
  144. return array($privkey, $pubkey);
  145. }
  146. //test php_SM2Sign
  147. function test_php_SM2SignAndSM2Verify(){
  148. $php_func = 'php_SM2Sign';
  149. echo "--------$php_func--------\n";
  150. $data = "this is test 这是测试!this is test 这是测试!";//数据有中文则需要注意字符编码的问题
  151. $sm2keys = test_php_SM2Genkey();//0 privkey 1 pubkey
  152. //签名
  153. $recode = $php_func($data, $redata,$sm2keys[0],$sm2keys[1]);
  154. echo "recode:$recode \n";
  155. echo "signValue: ".string2hex($redata)." len:".strlen($redata)." \n";
  156. //php_SM2Sign接口返回的签名值是RS格式,以下示例转DER编码
  157. $php_func = 'php_SM2FormatConvert';
  158. $recode = $php_func(202,$redata, $der);
  159. echo "recode:$recode \n";
  160. echo "signValue DER: ".string2hex($der)." len:".strlen($der)." \n";
  161. //验签
  162. $php_func = 'php_SM2Verify';
  163. $recode = $php_func($data, $redata,$sm2keys[1]);
  164. echo "recode:$recode \n";
  165. //php_SM2Verify 接收的签名值是RS格式,若对方给的签名值是DER格式
  166. //则通过如下方式转换,然后再验签
  167. $php_func = 'php_SM2FormatConvert';
  168. $recode = $php_func(201,$der, $rs);
  169. echo "recode:$recode \n";
  170. echo "signValue RS: ".string2hex($rs)." len:".strlen($rs)." \n";
  171. }
  172. //test php_SM2SignHash php_SM2VerifyHash
  173. function test_php_SM2SignHashAndSM2VerifyHash(){
  174. $php_func = 'php_SM2SignHash';
  175. echo "--------$php_func--------\n";
  176. $data = "this is test 这是测试!this is test 这是测试!";
  177. $sm2keys = test_php_SM2Genkey();//0 privkey 1 pubkey
  178. $php_func = 'php_SM3Crypt';
  179. $recode = $php_func($data,$sm3hash);
  180. echo "recode:$recode \n";
  181. echo "sm3hash: ".string2hex($sm3hash)." len:".strlen($sm3hash)." \n";
  182. $php_func = 'php_SM2SignHash';
  183. $recode = $php_func($sm3hash, $redata,$sm2keys[0]);
  184. echo "recode:$recode \n";
  185. echo "signValue: ".string2hex($redata)." len:".strlen($redata)." \n";
  186. $php_func = 'php_SM2VerifyHash';
  187. $recode = $php_func($sm3hash, $redata,$sm2keys[1]);
  188. echo "recode:$recode \n";
  189. }
  190. //test php_SM2Encrypt php_SM2Decrypt
  191. function test_php_SM2EncryptAndSM2Decrypt(){
  192. $php_func = 'php_SM2Encrypt';
  193. echo "--------$php_func--------\n";
  194. $data = "this is test 这是测试!this is test 这是测试!";
  195. $sm2keys = test_php_SM2Genkey();//0 privkey 1 pubkey
  196. //加密
  197. $recode = $php_func($data, $cipher,$sm2keys[1]);
  198. echo "recode:$recode \n";
  199. echo "cipher: ".string2hex($cipher)." len:".strlen($cipher)." \n";
  200. //php_SM2Encrypt接口返回的密文值是c1c3c2格式,以下示例转DER编码
  201. $php_func = 'php_SM2FormatConvert';
  202. $recode = $php_func(102,$cipher, $der);
  203. echo "recode:$recode \n";
  204. echo "cipher DER: ".string2hex($der)." len:".strlen($der)." \n";
  205. //解密
  206. $php_func = 'php_SM2Decrypt';
  207. $recode = $php_func($cipher, $plain,$sm2keys[0]);
  208. echo "recode:$recode \n";
  209. echo "plain: $plain\n";
  210. //php_SM2Decrypt 接收密文值是c1c3c2格式,若对方给的密文值是DER格式
  211. //则通过如下方式转换,然后再传入接口解密
  212. $php_func = 'php_SM2FormatConvert';
  213. $recode = $php_func(101,$der, $c1c3c2);
  214. echo "recode:$recode \n";
  215. echo "cipher c1c3c2: ".string2hex($c1c3c2)." len:".strlen($c1c3c2)." \n";
  216. }
  217. //test php_HextoAsc php_AsctoHex
  218. function test_php_HextoAscAndAsctoHex(){
  219. $php_func = 'php_AsctoHex';
  220. echo "--------$php_func--------\n";
  221. $data = "this is test 这是测试!this is test 这是测试!";
  222. $recode = $php_func($data, $hex);
  223. echo "recode:$recode \n";
  224. echo "hex: $hex len:".strlen($hex)." \n";
  225. $php_func = 'php_HextoAsc';
  226. $recode = $php_func($hex, $asc);
  227. echo "recode:$recode \n";
  228. echo "plain: $asc\n";
  229. }
  230. //test php_base64_decode php_base64_encode
  231. function test_php_base64_decodeAndbase64_encode(){
  232. $php_func = 'php_base64_encode';
  233. echo "--------$php_func--------\n";
  234. $data = "this is test 这是测试!this is test 这是测试!";
  235. $recode = $php_func($data, $base64);
  236. echo "recode:$recode \n";
  237. echo "base64char: $base64 len:".strlen($base64)." \n";
  238. $php_func = 'php_base64_decode';
  239. $recode = $php_func($base64, $orgin);
  240. echo "recode:$recode \n";
  241. echo "plain: $orgin\n";
  242. }
  243. //test
  244. function test_php_SM2FormatConvert(){
  245. $php_func = 'php_SM2FormatConvert';
  246. echo "--------$php_func--------\n";
  247. //hex编码的DER密文
  248. $data =
  249. "308198021F5A9D5395EE7A52463E07727CAEA3001A3D95ADF105992B3F8430C0B63D5272022011BC87B586EA976FF2A8009393FE3F71FEBD11FD249F59796054DB66B4789C6D04201D3FAEF883C8D839DDF36FC083B125DE65D8E58B3AACEB97CEBCF529A8F0C2A7043119C9E04B8DB90E6CC0898A85665DA1FA990B4DB6197AE55674D68C3AB5328D408D0B7EBAE99ED6C8243FED2A18C024545A";
  250. //先解码再转der格式(如果是base64编码,则用base64解码)
  251. $php_HextoAsc = 'php_HextoAsc';
  252. $recode = $php_HextoAsc($data, $dataAsc);
  253. $recode = $php_func(101,$dataAsc, $c1c3c2);
  254. echo "recode:$recode \n";
  255. echo "c1c3c2: ".string2hex($c1c3c2)." len:".strlen($c1c3c2)." \n";
  256. }
  257. //test SM4CBCCrypt
  258. function test_php_SM4CBCCrypt(){
  259. $php_func = 'php_SM4CBCCrypt';
  260. echo "--------$php_func--------\n";
  261. $data = "this is a test! 这是个测试!";
  262. $plainkey = "1234567890123456";//如果是hex编码,务必解码
  263. //接口是密文key,将明文key加密后再传入php_SM4CBCCrypt进行数据加密
  264. //不必要每次都加密key,建议手工加密一次,将key密文保存使用,key明文保存至安全的地方
  265. //若是会话级别的key则自行定策略
  266. $php_CryptKey = 'php_CryptKey';
  267. $recode = $php_CryptKey(0, $plainkey,$key);
  268. echo "recode:$recode \n";
  269. //打印key密文
  270. $php_hex = 'php_AsctoHex';
  271. $recode = $php_hex($key, $hex);
  272. echo "recode:$recode \n";
  273. echo "hex: $hex len:".strlen($hex)." \n";
  274. $iv = "1234567812345678";
  275. //加密
  276. $recode = $php_func(0,$data,$redata,$key,$iv);
  277. echo "recode:$recode \n";
  278. echo "redata: ".string2hex($redata)." len:".strlen($redata)." \n";
  279. //解密
  280. $recode = $php_func(1,$redata,$dedata,$key,$iv);
  281. echo "recode:$recode \n";
  282. echo "plain: ".$dedata." \n";
  283. }
  284. function run_test(){
  285. //先初始化接口,初始化成功后,可重复调用加解密接口
  286. test_php_crypto_init();
  287. test_php_SM4Crypt1();
  288. echo "\n";
  289. test_php_SM4Crypt2();
  290. echo "\n";
  291. test_php_CryptFile();
  292. echo "\n";
  293. test_php_CryptKey();
  294. echo "\n";
  295. test_php_SM3Crypt();
  296. echo "\n";
  297. test_php_MACCrypt();
  298. echo "\n";
  299. test_php_CryptLmkMac();
  300. echo "\n";
  301. test_php_SM2SignAndSM2Verify();
  302. echo "\n";
  303. test_php_SM2SignHashAndSM2VerifyHash();
  304. echo "\n";
  305. test_php_SM2EncryptAndSM2Decrypt();
  306. echo "\n";
  307. test_php_HextoAscAndAsctoHex();
  308. echo "\n";
  309. test_php_base64_decodeAndbase64_encode();
  310. echo "\n";
  311. test_php_SM2FormatConvert();
  312. echo "\n";
  313. test_php_SM4CBCCrypt();
  314. }
  315. run_test();
  316. ?>