php 双向加密解密
2018-11-29 PHP 1614
/** 用法: Secret::encrypt($str,$key); Secret::decrypt($str,$key); */ class Secret { public static $key = 'jatvsjat'; public static function encrypt($data, $key='123456') { $key = self::$key; $data = base64_encode($data); $x = 0; $len = strlen($data); $l = strlen($key); $char = ''; for ($i = 0; $i < $len; $i++) { if ($x == $l) $x = 0; $char .= substr($key, $x, 1); $x++; } $str = sprintf('%010d', $expire ? $expire + time():0); for ($i = 0; $i < $len; $i++) { $str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1)))%256); } return str_replace(array('+','/','='),array('-','_',''),base64_encode($str)); } public static function decrypt($data, $key='123456') { $key = self::$key; $data = str_replace(array('-','_'),array('+','/'),$data); $mod4 = strlen($data) % 4; if ($mod4) { $data .= substr('====', $mod4); } $data = base64_decode($data); $expire = substr($data,0,10); $data = substr($data,10); if($expire > 0 && $expire < time()) { return ''; } $x = 0; $len = strlen($data); $l = strlen($key); $char = $str = ''; for ($i = 0; $i < $len; $i++) { if ($x == $l) $x = 0; $char .= substr($key, $x, 1); $x++; } for ($i = 0; $i < $len; $i++) { if (ord(substr($data, $i, 1))<ord(substr($char, $i, 1))) { $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); }else{ $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); } } return base64_decode($str); } }
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论