Test.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. /**
  5. * 手机短信接口
  6. */
  7. class Test extends Api
  8. {
  9. protected $noNeedLogin = '*';
  10. protected $noNeedRight = '*';
  11. //验签,2048位,265截取
  12. public function verifysign(){
  13. //解密签名开始
  14. $sign = $this->request->request('sign','','trim');
  15. if(empty($sign)){
  16. $this->error('缺少签名');
  17. }
  18. $sign = base64_decode($sign);
  19. $private_key_str = config('app_rsa.private_key');
  20. $private_key = "-----BEGIN RSA PRIVATE KEY-----" .PHP_EOL.
  21. wordwrap($private_key_str, 64, PHP_EOL, true) .
  22. PHP_EOL."-----END RSA PRIVATE KEY-----";
  23. $signgetdata = []; //被解密出来的数据
  24. $split_len = 256;
  25. $sign_split = str_split($sign, $split_len);
  26. foreach($sign_split as $key => $sign_val){
  27. $signgetdata_child = null;
  28. openssl_private_decrypt($sign_val, $signgetdata_child, $private_key); // 使用私钥解密数据
  29. $signgetdata[] = $signgetdata_child;
  30. }
  31. $signgetdata = implode('',$signgetdata);
  32. if (!$signgetdata) {
  33. $this->error('签名错误1');
  34. }
  35. dump($signgetdata);
  36. //解密签名结束
  37. //接收到的参数,组成我自己的验签体string
  38. $request_all = $this->request->request();
  39. unset($request_all['s']);
  40. unset($request_all['sign']);
  41. ksort($request_all);
  42. $request_str = '';
  43. foreach($request_all as $key => $param){
  44. $request_str .= $key.'='.$param.'&';
  45. }
  46. $request_str .= 'signkey=F_dC923_35270PdsIIUIUTRERYTYYU';
  47. dump($request_str);
  48. //作对比
  49. if($request_str != $signgetdata){
  50. $this->error('验签错误');
  51. }
  52. echo '验签正确';
  53. }
  54. //验签
  55. public function verifysign_old(){
  56. //解密签名
  57. $sign = $this->request->request('sign','','trim');
  58. if(empty($sign)){
  59. $this->error('缺少签名');
  60. }
  61. $sign = base64_decode($sign);
  62. $private_key_str = config('app_rsa.private_key');
  63. $private_key = "-----BEGIN RSA PRIVATE KEY-----" .PHP_EOL.
  64. wordwrap($private_key_str, 64, PHP_EOL, true) .
  65. PHP_EOL."-----END RSA PRIVATE KEY-----";
  66. $signgetdata = ''; //被解密出来的数据
  67. openssl_private_decrypt($sign, $signgetdata, $private_key); // 使用私钥解密数据
  68. if (!$signgetdata) {
  69. $this->error('签名错误');
  70. }
  71. //dump($signgetdata);
  72. //接收到的参数
  73. $request_all = $this->request->request();
  74. unset($request_all['s']);
  75. unset($request_all['sign']);
  76. ksort($request_all);
  77. $request_all = http_build_query($request_all);
  78. $request_all .= '&signkey=F_dC923_35270PdsIIUIUTRERYTYYU';
  79. //dump($request_all);
  80. //作对比
  81. if($request_all != $signgetdata){
  82. $this->error('验签错误');
  83. }
  84. //echo '验签正确';
  85. return true;
  86. }
  87. //加密
  88. public function jiami(){
  89. $request_all = $this->request->request();
  90. unset($request_all['s']);
  91. // dump($request_all);
  92. ksort($request_all);
  93. // dump($request_all);
  94. $request_all = http_build_query($request_all);
  95. $request_all .= '&signkey=F_dC923_35270PdsIIUIUTRERYTYYU';
  96. // echo '加密字符串';
  97. // echo $request_all;
  98. $public_key_str = config('app_rsa.public_key');
  99. echo strlen($public_key_str);exit;
  100. $public_key = "-----BEGIN PUBLIC KEY-----" .PHP_EOL.
  101. wordwrap($public_key_str, 64, PHP_EOL, true) .
  102. PHP_EOL."-----END PUBLIC KEY-----";
  103. $sign = '';
  104. openssl_public_encrypt($request_all,$sign,$public_key);
  105. $sign = base64_encode($sign);
  106. echo '获得签名';
  107. dump($sign);
  108. }
  109. /**
  110. *
  111. * @return void
  112. */
  113. public function test1()
  114. {
  115. echo '<pre>';var_dump(1);exit;
  116. }
  117. }