passport_decrypt-Passport 解密函数

/**
* 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;
// $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
// 与 $encrypt_key 的第 $ctr + 1 位取异或。然后 $ctr = $ctr + 1
$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
}

// 返回 $tmp 的值作为结果
return $tmp;

}

来源:Discuz API

Popularity: 2%

No Responses to “passport_decrypt-Passport 解密函数”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>