ProtocolsEvent.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * 加解密协议
  4. * Class Protocols
  5. */
  6. class ProtocolsEvent
  7. {
  8. /**
  9. * Notes:加密输出
  10. * User: wsc
  11. * Time: 2019/12/28 10:37
  12. * @param $str
  13. * @return string
  14. */
  15. public static function encode($str){
  16. $str = base64_encode($str);
  17. $key = floor(strlen($str)/2)-1;
  18. $out = substr($str,0,$key).substr(md5(time()),0,16).substr($str,$key,strlen($str));
  19. $out = substr($out,0,1).substr(md5(time()),0,1).substr($out,1,strlen($out));
  20. return $out;
  21. }
  22. /**
  23. * Notes:解码输出
  24. * User: wsc
  25. * Time: 2019/12/28 10:38
  26. * @param $str
  27. * @return bool|string
  28. */
  29. private static function decode($str){
  30. $str = str_replace(" ","+",$str);
  31. $sign = substr($str,0,1).substr($str,2,strlen($str));
  32. $sign = substr($sign,0,floor((strlen($sign)-16)/2)-1).substr($sign,floor((strlen($sign)-16)/2)+15,strlen($sign));
  33. $sign = base64_decode($sign);
  34. return $sign;
  35. }
  36. /**
  37. * Notes:api接口输出封装
  38. * User: wsc
  39. * Time: 2019/12/28 10:49
  40. * @param $str
  41. */
  42. public static function apiEncode($str){
  43. }
  44. /**
  45. * Notes:api接口解码
  46. * User: wsc
  47. * Time: 2019/12/28 10:42
  48. * @param $str
  49. */
  50. public static function apiDecode($str){
  51. $sign = self::decode($str);
  52. $sign = trim($sign,'&');
  53. $sign = explode('&',$sign);
  54. foreach ($sign as $row){
  55. $temp = explode('=',$row,2);
  56. $_REQUEST[$temp[0]] = $temp[1];
  57. $_POST[$temp[0]] = $temp[1];
  58. $_GET[$temp[0]] = $temp[1];
  59. }
  60. unset($_REQUEST['param']);
  61. unset($_POST['param']);
  62. unset($_GET['param']);
  63. }
  64. /**
  65. * Notes:新api接口解码
  66. * User: wsc
  67. * Time: 2019/12/28 10:42
  68. * @param $str
  69. */
  70. public static function apiDecodeNew($str){
  71. $getParams = [];
  72. $sign = self::decode($str);
  73. $sign = trim($sign,'&');
  74. $sign = explode('&',$sign);
  75. foreach ($sign as $row){
  76. $temp = explode('=',$row,2);
  77. $getParams[$temp[0]] = $temp[1];
  78. }
  79. return $getParams;
  80. }
  81. /**
  82. * Notes:
  83. * User: wsc
  84. * Time: 2019/12/28 10:45
  85. * @param $str
  86. * @return bool|mixed|string
  87. */
  88. public static function wsDecode($str){
  89. $res = self::decode($str);
  90. $res = json_decode($res,true);
  91. return $res;
  92. }
  93. }