1.   阿里云服务器1888元代金券领取
  2.   本网站所有源码包解压密码:www.youhutong.com
登录  帮助问答

PHP字符串编码解码函数,字符串安全加密解密方法!(可用于用户自动登录安全实现加密方法)

小川 技术文章
郑重声明:
  本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,否则后果请用户自负,下载后请24小时内删除。 本站所有内容均不能保证其完整性,不提供任何技术支持,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用。

PHP字符串编码解码函数,字符串安全加密解密方法:


方法1:
/**
 * 字符串编码解码函数
 * @param: string $string 字符串
 * @param: string $operation 分ENCODE和DECODE两种方式
 * @param: string $key 算法密钥
 * @param: int $expiry 有效期(单位秒)
 * return: string
 */
function string_code($string, $operation = 'DECODE', $key = '', $expiry = 0) {

	if($operation == 'DECODE'){
		$string1 = substr($string, 0, floor(strlen($string)/3));
		$string2 = str_replace($string1, "", $string);
		$string = $string2.$string1;
		$string = str_replace(["-", "_"], ["/", "+"], $string);
	}
	
	$ckey_length = 4;
	$key = md5($key);
	$keya = md5(substr($key, 0, 16));
	$keyb = md5(substr($key, 16, 16));
	$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): 
	substr(md5(microtime()), -$ckey_length)) : '';

	$cryptkey = $keya.md5($keya.$keyc);
	$key_length = strlen($cryptkey);

	$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : 
	sprintf('%010d', $expiry ? $expiry + time() : 0).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]);
	}

	for($j = $i = 0; $i < 256; $i++) {
		$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($operation == 'DECODE') {
		if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) 
		&& substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
			return substr($result, 26);
		} else {
			return ''; 
		}
	} else {
		$result = $keyc.str_replace('=', '', base64_encode($result));
		$result = str_replace(["/", "+"], ["-", "_"], $result);
		$result1 = substr($result, floor(strlen($result)/3)*(-1));
		$result2 = str_replace($result1, "", $result);
		$result = $result1.$result2;
		return $result;
	}
}




// 可以用来自动登录用户等等
//1 、加密数据,然后存入cookie
$code = string_code('5|小川编程', 'ENCODE', 'CtLgVd7f6r1xs', 2592000);
cookie(md5(config('cookie_key')), $code, 2592000);

//2、取出cookie值,然后解密。
$cookie = cookie(md5(config('cookie_key')));
$value = string_code($cookie, 'DECODE', 'CtLgVd7f6r1xs');
$value = explode('|', $value, 2);
if(count($value) != 2) $this->error('自动登录失败');
list($uid, $lastTime) = $value;
if(!empty($uid)){
	//有了用户ID,辅助值,我们就可以判断用户存在与否等等、	
}else{
     $this->error('自动登录失败');   
}


方法2
/**
 * edauth高效可逆随机加密函数
 * @param string $string    明文 或 密文
 * @param $operation:true表示加密,false表示解密
 * @param $key: 密匙
 * @param $outtime:密文有效期, 单位为秒
 * @param $entype:加密方式 有md5和sha1两种 加密解密需要统一使用同一种方式才能正确还原明文
 * @return string 
 */
function edauth($string, $operation = true, $outtime = 0, $entype = 'md5') {
    $key = md5($key ? $key : C('AUTH_KEY'));
    $key_length = 4;
    if ($entype == 'md5') { //使用md5方式
        $long_len = 32;
        $half_len = 16;
        $entype == 'md5';
    } else { //使用sha1方式
        $long_len = 40;
        $half_len = 20;
        $entype == 'sha1';
    }

    $key      = $key != '' ? $key : substr(md5($_SERVER['DOCUMENT_ROOT'] 
                . C('AUTH_KEY') . $_SERVER['REMOTE_ADDR']), 0, 30);

    $fixedKey = hash($entype, $key);

    $egiskeys = md5(substr($fixedKey, $half_len, $half_len));

    $runtoKey = $key_length ? ($operation ? 
                substr(hash($entype, microtime(true)), -$key_length) :
                substr($string, 0, $key_length)) : '';

    $keys     = hash($entype, substr($runtoKey, 0, $half_len) . substr($fixedKey, 0, $half_len) 
               . substr($runtoKey, $half_len) . substr($fixedKey, $half_len));

    $string   = $operation ? sprintf('%010d', $outtime ? $outtime + time() : 0) 
               . substr(md5($string . $egiskeys), 0, $half_len) 
               . $string : base64_decode(substr($string, $key_length));

    $i = 0;

    $result   = '';

    $string_length = strlen($string);

    for ($i = 0; $i < $string_length; $i++) {

        $result .= chr(ord($string{$i}) ^ ord($keys{$i % $long_len}));

    }

    if ($operation) {

        return $runtoKey . str_replace('=', '', base64_encode($result));

    } else {
        
        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && 
            substr($result, 10, $half_len) == substr(md5(substr($result, $half_len + 10) 
                . $egiskeys), 0, $half_len)) {


            return substr($result, $half_len + 10);

        } else {

            return '';

        }
    }
}


 浏览器启用弹出窗口过滤功能,将无法跳转到下载页。在浏览器地址栏右边符号提示处点击允许就可以了!

转载请注明来源地址:小川编程 » https://www.youhutong.com/index.php/article/index/110.html


  1、本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,下载后请24小时内删除。
  2、本站所有内容均不能保证其完整性,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用
  3、本站联系方式Email:admin@youhutong.com ,收到邮件会第一时间处理。
  4、如侵犯到任何版权问题,请立即告知本站(立即在线告知),本站将及时删除并致以最深的歉意
( 0 )个小伙伴在吐槽
    登录帐号  如果已经登录请刷新! 发表我的评论
    表情