6682efd9272b3950838484b14c0efe818f09b30a.svn-base 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php namespace Maze\Security;
  2. class Internal
  3. {
  4. /**
  5. * key
  6. *
  7. * @var string
  8. */
  9. static private $key = 'qwertyuiop12345asdfghjkl67890zxcvbnm';
  10. /**
  11. * encode
  12. * @param string $string
  13. * @param string $key
  14. *
  15. * @return string
  16. */
  17. static public function encode($string, $key="")
  18. {
  19. $ckey_length = 5;
  20. if(!$key) $key = md5(self::$key);
  21. $keya = md5(substr($key, 0, 16));
  22. $keyb = md5(substr($key, 16, 16));
  23. $keyc = $ckey_length ? substr(md5(microtime()), -$ckey_length) : '';//md5串后4位,每次不一样
  24. $cryptkey = $keya.md5($keya.$keyc);//两个md5串
  25. $key_length = strlen($cryptkey);//64
  26. $string = sprintf('%010d', time()).substr(md5($string.$keyb), 0, 16).$string;
  27. $string_length = strlen($string);
  28. $result = '';
  29. $box = range(0, 255);
  30. $rndkey = array();
  31. for($i = 0; $i <= 255; $i++)
  32. {
  33. $rndkey[$i] = ord($cryptkey[$i % $key_length]);//生成一个255个元素的数组
  34. }
  35. for($j = $i = 0; $i < 256; $i++)
  36. {
  37. //将$box数组转换为无序并且个数不变的数据
  38. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  39. $tmp = $box[$i];
  40. $box[$i] = $box[$j];
  41. $box[$j] = $tmp;
  42. }
  43. for($a = $j = $i = 0; $i < $string_length; $i++)
  44. {
  45. $a = ($a + 1) % 256;
  46. $j = ($j + $box[$a]) % 256;
  47. $tmp = $box[$a];
  48. $box[$a] = $box[$j];
  49. $box[$j] = $tmp;
  50. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  51. }
  52. return $keyc.str_replace('=', '', self::base64_encode($result));
  53. }
  54. /**
  55. * decode
  56. * @param string $string
  57. * @param string $key
  58. *
  59. * @return string
  60. */
  61. static public function decode($string, $key="")
  62. {
  63. $ckey_length = 5;
  64. if(!$key) $key = md5(self::$key);
  65. $keya = md5(substr($key, 0, 16));
  66. $keyb = md5(substr($key, 16, 16));
  67. $keyc = $ckey_length ? substr($string, 0, $ckey_length) : '';//和encrypt时的$keyc一样
  68. $cryptkey = $keya.md5($keya.$keyc);
  69. $key_length = strlen($cryptkey);
  70. $string = self::base64_decode(substr($string, $ckey_length)) ;
  71. $string_length = strlen($string);
  72. $result = '';
  73. $box = range(0, 255);
  74. $rndkey = array();
  75. for($i = 0; $i <= 255; $i++)
  76. {
  77. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  78. }
  79. for($j = $i = 0; $i < 256; $i++)
  80. {
  81. //和encrypt时的$box一样
  82. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  83. $tmp = $box[$i];
  84. $box[$i] = $box[$j];
  85. $box[$j] = $tmp;
  86. }
  87. for($a = $j = $i = 0; $i < $string_length; $i++)
  88. {
  89. //核心操作,解密
  90. $a = ($a + 1) % 256;
  91. $j = ($j + $box[$a]) % 256;
  92. $tmp = $box[$a];
  93. $box[$a] = $box[$j];
  94. $box[$j] = $tmp;
  95. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  96. }
  97. if(substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16))
  98. {
  99. return substr($result, 26);
  100. }
  101. else
  102. {
  103. return '';
  104. }
  105. }
  106. /**
  107. * base64_encode
  108. * @param string $string
  109. *
  110. * @return string
  111. */
  112. static function base64_encode($string)
  113. {
  114. if(!$string)
  115. {
  116. return false;
  117. }
  118. $encodestr = base64_encode($string);
  119. $encodestr = str_replace(array('+','/'),array('-','_'),$encodestr);
  120. return $encodestr;
  121. }
  122. /**
  123. * base64_decode
  124. * @param string $string
  125. *
  126. * @return string
  127. */
  128. static function base64_decode($string)
  129. {
  130. if(!$string)
  131. {
  132. return false;
  133. }
  134. $string = str_replace(array('-','_'),array('+','/'),$string);
  135. $decodestr = base64_decode($string);
  136. return $decodestr;
  137. }
  138. }