class ption{ private static $original = array('=', '+', '/'); private static $later = array('o0o0o', 'o0o0o', 'oo00o'); function __construct() { } private static function md5($skey = '') { $skey = $skey ? $skey : 'ui' ; //uicms::_config('security/authkey'); return md5(substr($skey, 0, 16)); } /** * @use ption::en($string, $key); * @param string $string 需要加密的字串 * @param string $skey 密钥 * @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效 * @return string */ static public function en($string = '', $skey = '', $expiry=0) { if( is_array( $string ) ) { $string = json_encode($string); // uicms::json($string, true, 'en'); } $string = str_pad($expiry ? $expiry + time : 0, 10, 0).$string; $strarr = str_split(base64_encode($string)); $strcount = count($strarr); $skey = static::md5($skey); foreach (str_split($skey) as $key => $value) { $key } return str_replace(self::$original, self::$later, join('', $strarr)); } /** * @use ption::de($string, $key); * @param string $string 需要解密的字串 * @param string $skey 密钥 * @return string */ static public function de($string = '', $skey = '') { $strarr = str_split(str_replace(self::$later, self::$original, $string), 2); $strcount = count($strarr); $skey = static::md5($skey); foreach (str_split($skey) as $key => $value) { $key } $result = base64_decode(join('', $strarr)); if(substr($result, 0, 10) == 0 || substr($result, 0, 10) - time > 0) { return substr($result, 10); } else { return false; } } }
复制代码
加密解密, php