PHP对url 加密解密

3,556次阅读

/**
* @param $txt
* @param string $key
* @return string
* @author: Alan 明宇 <admin@tianfayl.com>
* @name: lock_url
* @describe: 加密函数
*/
public function lock_url($txt,$key='www.luckyhhy.cn') {
	$txt = $txt.$key;
	$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
	$nh = rand(0,64);
	$ch = $chars[$nh];
	$mdKey = md5($key.$ch);
	$mdKey = substr($mdKey,$nh%8, $nh%8+7);
	$txt = base64_encode($txt);
	$tmp = '';
	$i=0;
	$j=0;
	$k = 0;
	for ($i=0; $i<strlen($txt); $i++) {$k = $k == strlen($mdKey) ? 0 : $k;
		$j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
		$tmp .= $chars[$j];
	}
	return urlencode(base64_encode($ch.$tmp));
}
/**
* @param $txt
* @param string $key
* @return string
* @author: Alan 明宇 <admin@tianfayl.com>
* @name: unlock_url
* @describe: 解密函数
*/
public function unlock_url($txt,$key='www.luckyhhy.cn') {$txt = base64_decode(urldecode($txt));
	$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
	$ch = $txt[0];
	$nh = strpos($chars,$ch);
	$mdKey = md5($key.$ch);
	$mdKey = substr($mdKey,$nh%8, $nh%8+7);
	$txt = substr($txt,1);
	$tmp = '';
	$i=0;
	$j=0;
	$k = 0;
	for ($i=0; $i<strlen($txt); $i++) {$k = $k == strlen($mdKey) ? 0 : $k;
		$j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
		while ($j<0) $j+=64;
		$tmp .= $chars[$j];
	}
	return trim(base64_decode($tmp),$key);
}

 

微信扫描下方的二维码阅读本文

PHP 对 url 加密解密

 
Alan明宇
版权声明:本站原创文章,由 Alan明宇 2022-01-12发表,共计1158字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。