代码收藏

收集网站开发时常用到的代码(asp,php,js等)

« isIE-判断是否是IE浏览器passport_decrypt-Passport 解密函数 »

passport_encrypt-Passport 加密函数

  /**
  * 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() 函数返回值的 base64 编码结果
    return base64_encode(passport_key($tmp, $key));

  }


来源:Discuz API
  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Spirit Build 80605

Copyright 2008 ITlearner. Some Rights Reserved.