123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php namespace Maze\Security;
- class Internal
- {
- /**
- * key
- *
- * @var string
- */
- static private $key = 'qwertyuiop12345asdfghjkl67890zxcvbnm';
-
- /**
- * encode
- * @param string $string
- * @param string $key
- *
- * @return string
- */
- static public function encode($string, $key="")
- {
- $ckey_length = 5;
-
- if(!$key) $key = md5(self::$key);
-
- $keya = md5(substr($key, 0, 16));
- $keyb = md5(substr($key, 16, 16));
- $keyc = $ckey_length ? substr(md5(microtime()), -$ckey_length) : '';//md5串后4位,每次不一样
- $cryptkey = $keya.md5($keya.$keyc);//两个md5串
- $key_length = strlen($cryptkey);//64
- $string = sprintf('%010d', time()).substr(md5($string.$keyb), 0, 16).$string;
- $string_length = strlen($string);
- $result = '';
- $box = range(0, 255);
- $rndkey = array();
- for($i = 0; $i <= 255; $i++)
- {
- $rndkey[$i] = ord($cryptkey[$i % $key_length]);//生成一个255个元素的数组
- }
- for($j = $i = 0; $i < 256; $i++)
- {
- //将$box数组转换为无序并且个数不变的数据
- $j = ($j + $box[$i] + $rndkey[$i]) % 256;
- $tmp = $box[$i];
- $box[$i] = $box[$j];
- $box[$j] = $tmp;
- }
- for($a = $j = $i = 0; $i < $string_length; $i++)
- {
- $a = ($a + 1) % 256;
- $j = ($j + $box[$a]) % 256;
- $tmp = $box[$a];
- $box[$a] = $box[$j];
- $box[$j] = $tmp;
- $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
- }
-
- return $keyc.str_replace('=', '', self::base64_encode($result));
- }
- /**
- * decode
- * @param string $string
- * @param string $key
- *
- * @return string
- */
- static public function decode($string, $key="")
- {
- $ckey_length = 5;
-
- if(!$key) $key = md5(self::$key);
-
- $keya = md5(substr($key, 0, 16));
- $keyb = md5(substr($key, 16, 16));
- $keyc = $ckey_length ? substr($string, 0, $ckey_length) : '';//和encrypt时的$keyc一样
- $cryptkey = $keya.md5($keya.$keyc);
- $key_length = strlen($cryptkey);
- $string = self::base64_decode(substr($string, $ckey_length)) ;
- $string_length = strlen($string);
- $result = '';
- $box = range(0, 255);
- $rndkey = array();
- for($i = 0; $i <= 255; $i++)
- {
- $rndkey[$i] = ord($cryptkey[$i % $key_length]);
- }
- for($j = $i = 0; $i < 256; $i++)
- {
- //和encrypt时的$box一样
- $j = ($j + $box[$i] + $rndkey[$i]) % 256;
- $tmp = $box[$i];
- $box[$i] = $box[$j];
- $box[$j] = $tmp;
- }
- for($a = $j = $i = 0; $i < $string_length; $i++)
- {
- //核心操作,解密
- $a = ($a + 1) % 256;
- $j = ($j + $box[$a]) % 256;
- $tmp = $box[$a];
- $box[$a] = $box[$j];
- $box[$j] = $tmp;
- $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
- }
- if(substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16))
- {
- return substr($result, 26);
- }
- else
- {
- return '';
- }
- }
-
- /**
- * base64_encode
- * @param string $string
- *
- * @return string
- */
- static function base64_encode($string)
- {
- if(!$string)
- {
- return false;
- }
- $encodestr = base64_encode($string);
- $encodestr = str_replace(array('+','/'),array('-','_'),$encodestr);
- return $encodestr;
- }
-
- /**
- * base64_decode
- * @param string $string
- *
- * @return string
- */
- static function base64_decode($string)
- {
- if(!$string)
- {
- return false;
- }
- $string = str_replace(array('-','_'),array('+','/'),$string);
- $decodestr = base64_decode($string);
- return $decodestr;
- }
- }
|