discuz的加密算法

<?php
/**
* Passport 加密函数
*
* @param                string                等待加密的原字串
* @param                string                私有密匙(用于解密和加密)
*
* @return        string                原字串经过私有密匙加密后的结果
*/
function passport_encrypt($txt, $key) {
	// 使用随机数发生器产生 0~32000 的值并 MD5()
	srand((double)microtime() * 1000000);
	$encrypt_key = md5(rand(0, 32000));
	// 变量初始化
	$ctr = 0;
	$tmp = '';
	// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
	for($i = 0; $i < strlen($txt); $i++) {
	// 如果 $ctr = $encrypt_key 的长度,则 $ctr 清零
	$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
	// $tmp 字串在末尾增加两位,其第一位内容为 $encrypt_key 的第 $ctr 位,
	// 第二位内容为 $txt 的第 $i 位与 $encrypt_key 的 $ctr 位取异或。然后 $ctr = $ctr + 1
	$tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
	}
	// 返回结果,结果为 passport_key() 函数返回值的 base65 编码结果
	return base64_encode(passport_key($tmp, $key));
}
/**
* Passport 解密函数
*
* @param                string                加密后的字串
* @param                string                私有密匙(用于解密和加密)
*
* @return        string                字串经过私有密匙解密后的结果
*/
function passport_decrypt($txt, $key) {
	// $txt 的结果为加密后的字串经过 base64 解码,然后与私有密匙一起,
	// 经过 passport_key() 函数处理后的返回值
	$txt = passport_key(base64_decode($txt), $key);
	// 变量初始化
	$tmp = '';
	// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
	for ($i = 0; $i < strlen($txt); $i++) {
	// $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
	// 与 $txt 的第 $i + 1 位取异或。然后 $i = $i + 1
	$tmp .= $txt[$i] ^ $txt[++$i];
	}
	// 返回 $tmp 的值作为结果
	return $tmp;
}

/**
* Passport 密匙处理函数
*
* @param                string                待加密或待解密的字串
* @param                string                私有密匙(用于解密和加密)
*
* @return        string                处理后的密匙
*/
function passport_key($txt, $encrypt_key) {
	// 将 $encrypt_key 赋为 $encrypt_key 经 md5() 后的值
	$encrypt_key = md5($encrypt_key);
	// 变量初始化
	$ctr = 0;
	$tmp = '';
	// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
	for($i = 0; $i < strlen($txt); $i++) 
	{
		// 如果 $ctr = $encrypt_key 的长度,则 $ctr 清零
		$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr; //R   轮
		// $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
		// 与 $encrypt_key 的第 $ctr + 1 位取异或。然后 $ctr = $ctr + 1
		$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];   //R 轮翻异或加密;
	}
	// 返回 $tmp 的值作为结果
	return $tmp;
}

$txt = "135_43_5_1";
$key = "123456";


$anwen = passport_encrypt($txt,$key);
echo $anwen."<hr>";
$mingwen = passport_decrypt($anwen, $key);
echo $mingwen;





?>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值