EncryptInOut.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Util;
  4. use Closure;
  5. class EncryptInOut
  6. {
  7. /**
  8. * 验证签名 API接口
  9. * @param \Illuminate\Http\Request $request
  10. * @param Closure $next
  11. */
  12. public function handle($request, Closure $next)
  13. {
  14. $path = $request->path();
  15. // 现在 $path 变量包含了当前请求的路径
  16. $arr=$request->all();
  17. $newarr=[];
  18. foreach ($arr as $value){
  19. // Util::WriteLog("encrypt",self::decrypt($value,$path));
  20. $temp=[];
  21. parse_str(self::decrypt($value,$path),$temp);
  22. $newarr=array_merge($newarr,$temp);
  23. break;
  24. }
  25. $request->replace($newarr);
  26. $response = $next($request);
  27. // 对返回内容进行加密处理
  28. $content = $response->getContent();
  29. $encryptedContent = self::encrypt($content,$path);
  30. // 将加密后的内容设置回响应
  31. $response->setContent($encryptedContent);
  32. // $response=$response->header('Access-Control-Allow-Origin', '*')
  33. // ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
  34. return $response;
  35. }
  36. public static function encrypt($str,$pwd){
  37. try {
  38. if (empty($str) || empty($pwd)) {
  39. # code...
  40. return '';
  41. }
  42. $pwd = urlencode(iconv("gbk", "UTF-8", $pwd));
  43. $str = urlencode(iconv("gbk", "UTF-8", $str));
  44. $prand = '';
  45. for ($i = 0; $i < strlen($pwd); $i++) {
  46. $prand = $prand . ord(mb_substr($pwd, $i, 1));
  47. }
  48. $sPos = floor(strlen($prand) / 5);
  49. $mult = intval(mb_substr($prand, $sPos, 1) . mb_substr($prand, $sPos * 2, 1) . mb_substr($prand, $sPos * 3, 1) . mb_substr($prand, $sPos * 4, 1) . mb_substr($prand, $sPos * 5, 1));
  50. $incr = round(strlen($pwd) / 2);
  51. $modu = pow(2, 31) - 1;
  52. if (strlen($mult) < 2) {
  53. # code...
  54. return '';
  55. }
  56. $salt = round(rand(0, 1000000000)) % 100000000;
  57. $prand = $prand . $salt;
  58. while (strlen($prand) > 10) {
  59. # code...
  60. $left = $prand;
  61. $num = 0;
  62. while (strlen($left) > 0) {
  63. # code...
  64. $temp = intval(mb_substr($left, 0, 10));
  65. $num = $num + $temp;
  66. $left = mb_substr($left, 10);
  67. }
  68. $prand = $num . '';
  69. }
  70. $prand = ($mult * intval($prand) + $incr) % $modu;
  71. $encStr = '';
  72. for ($i = 0; $i < strlen($str); $i++) {
  73. $encChr = intval(ord(mb_substr($str, $i, 1)) ^ floor(($prand / $modu) * 255));
  74. if ($encChr < 16) {
  75. # code...
  76. $encStr = $encStr . '0' . dechex($encChr);
  77. } else {
  78. $encStr = $encStr . dechex($encChr);
  79. }
  80. $prand = ($mult * intval($prand) + $incr) % $modu;
  81. }
  82. $salt = dechex($salt);
  83. while (strlen($salt) < 8) {
  84. # code...
  85. $salt = '0' . $salt;
  86. }
  87. $encStr = $encStr . $salt;
  88. }catch (\Exception $exception){
  89. Util::WriteLog('encrypt',[http_response_code()]);
  90. $encStr=$str;
  91. }
  92. return $encStr;
  93. }
  94. public static function decrypt($str, $pwd){
  95. if (empty($str)||empty($pwd)) {
  96. # code...
  97. return '';
  98. }
  99. if (strlen($str)<8||strlen($pwd)<=0) {
  100. # code...
  101. return '';
  102. }
  103. $pwd=urlencode(iconv("gbk", "UTF-8", $pwd));
  104. $prand='';
  105. for ($i=0; $i <strlen($pwd); $i++) {
  106. $prand=$prand.ord(mb_substr($pwd, $i, 1));
  107. }
  108. $sPos=floor(strlen($prand)/5);
  109. $mult=intval(mb_substr($prand,$sPos,1).mb_substr($prand,$sPos*2,1).mb_substr($prand,$sPos*3,1).mb_substr($prand,$sPos*4,1).mb_substr($prand,$sPos*5,1));
  110. $incr=round(strlen($pwd)/2);
  111. $modu=pow(2, 31)-1;
  112. $salt=hexdec(mb_substr($str,strlen($str)-8));
  113. $str=mb_substr($str,0,strlen($str)-8);
  114. $prand=$prand.$salt;
  115. while (strlen($prand)>10) {
  116. # code...
  117. $left=$prand;
  118. $num=0;
  119. while (strlen($left)>0) {
  120. # code...
  121. $temp=intval(mb_substr($left, 0,10));
  122. $num=$num+$temp;
  123. $left=mb_substr($left, 10);
  124. }
  125. $prand=$num.'';
  126. }
  127. $prand=($mult*intval($prand)+$incr)%$modu;
  128. $encStr='';
  129. for ($i=0; $i <strlen($str) ; $i+=2) {
  130. # code...
  131. $encChr=intval(hexdec(mb_substr($str, $i,2))^floor(($prand/$modu)*255));
  132. $encStr=$encStr.chr($encChr);
  133. $prand=($mult*intval($prand)+$incr)%$modu;
  134. }
  135. return urldecode($encStr);
  136. }
  137. }