discuz!的passport里面的加密解密函数貌似可以.

解决方案 »

  1.   

    $test_key = 'test';
    /**
    * test 加密函数
    *
    * @param string 等待加密的原字串
    * @param string 私有密匙(用于解密和加密)
    *
    * @return string 原字串经过私有密匙加密后的结果
    */
    function test_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++]);
    } // 返回结果,结果为 test_key() 函数返回值的 base64 编码结果
    return base64_encode(test_key($tmp, $key)); } /**
    * test 解密函数
    *
    * @param string 加密后的字串
    * @param string 私有密匙(用于解密和加密)
    *
    * @return string 字串经过私有密匙解密后的结果
    */
    function test_decrypt($txt, $key) { // $txt 的结果为加密后的字串经过 base64 解码,然后与私有密匙一起,
    // 经过 test_key() 函数处理后的返回值
    $txt = test_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; } /**
    * test 密匙处理函数
    *
    * @param string 待加密或待解密的字串
    * @param string 私有密匙(用于解密和加密)
    *
    * @return string 处理后的密匙
    */
    function test_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; } /**
    * test 信息(数组)编码函数
    *
    * @param array 待编码的数组
    *
    * @return string 数组经编码后的字串
    */
    function test_encode($array) { // 数组变量初始化
    $arrayenc = array(); // 遍历数组 $array,其中 $key 为当前元素的下标,$val 为其对应的值
    foreach($array as $key => $val) {
    // $arrayenc 数组增加一个元素,其内容为 "$key=经过 urlencode() 后的 $val 值"
    $arrayenc[] = $key.'='.urlencode($val);
    } // 返回以 "&" 连接的 $arrayenc 的值(implode),例如 $arrayenc = array('aa', 'bb', 'cc', 'dd'),
    // 则 implode('&', $arrayenc) 后的结果为 ”aa&bb&cc&dd"
    return implode('&', $arrayenc); }
    使用,记得包含上面的函数.$data = array
    (
    'A' => '测试1',
    'B' => 6,
    'C' => 8,
    'D' => 1,
    );
    $auth = test_encrypt(test_encode($data), $test_key);
    $verify = md5($auth.$test_key);//这里的验证根据自己的需要写了。结合cookie和session,可以做到相当安全。echo "<a href=abc.php?auth=$auth&verify=$verify>test</a>";abc.php
    if($_GET['verify'] != md5($_GET['auth'].$test_key))//这里的验证根据自己的需要写了。结合cookie和session,可以做到相当安全。
    {
    echo "请不要构造非法数据。";//执行你自己的错误处理.
    }
    else
    {
    parse_str(test_decrypt($_GET['auth'],$test_key));
    echo $A;
    echo $B;
    echo $C;
    echo $D;
      

  2.   

    谢谢beiliwenxiao之前看过这段代码记得应该会出现加号(+)和(/)的
    等回家我再测试一下吧 谢谢你!
      

  3.   

    这个加密只有大小写字母的。如果还有不要的字符:传值前,把你不要的字符替换为几个连续的字母或数字。比如+替换为000111。/替换为AAABBB。收到传值后,用000111把+替换回来。用AAABBB把/替换回来。
      

  4.   


    010);
      $spfunction7 = array (0x200000,0x4200002,0x4000802,0,0x800,0x4000802,0x200802,0x4200800,0x4200802,0x200000,0,0x4000002,0x2,0x4000000,0x4200002,0x802,0x4000800,0x200802,0x200002,0x4000800,0x4000002,0x4200000,0x4200800,0x200002,0x4200000,0x800,0x802,0x4200802,0x200800,0x2,0x4000000,0x200800,0x4000000,0x200800,0x200000,0x4000802,0x4000802,0x4200002,0x4200002,0x2,0x200002,0x4000000,0x4000800,0x200000,0x4200800,0x802,0x200802,0x4200800,0x802,0x4000002,0x4200802,0x4200000,0x200800,0,0x2,0x4200802,0,0x200802,0x4200000,0x800,0x4000002,0x4000800,0x800,0x200002);
      $spfunction8 = array (0x10001040,0x1000,0x40000,0x10041040,0x10000000,0x10001040,0x40,0x10000000,0x40040,0x10040000,0x10041040,0x41000,0x10041000,0x41040,0x1000,0x40,0x10040000,0x10000040,0x10001000,0x1040,0x41000,0x40040,0x10040040,0x10041000,0x1040,0,0,0x10040040,0x10000040,0x10001000,0x41040,0x40000,0x41040,0x40000,0x10041000,0x1000,0x40,0x10040040,0x1000,0x41040,0x10001000,0x40,0x10000040,0x10040000,0x10040040,0x10000000,0x40000,0x10001040,0,0x10041040,0x40040,0x10000040,0x10040000,0x10001000,0x10001040,0,0x10041040,0x41000,0x41000,0x1040,0x1040,0x40040,0x10000000,0x10041000);
      $masks = array (4294967295,2147483647,1073741823,536870911,268435455,134217727,67108863,33554431,16777215,8388607,4194303,2097151,1048575,524287,262143,131071,65535,32767,16383,8191,4095,2047,1023,511,255,127,63,31,15,7,3,1,0);  //create the 16 or 48 subkeys we will need
      $keys = des_createKeys ($key);
      $m=0;
      $len = strlen($message);
      $chunk = 0;
      //set up the loops for single and triple des
      $iterations = ((count($keys) == 32) ? 3 : 9); //single or triple des
      if ($iterations == 3) {$looping = (($encrypt) ? array (0, 32, 2) : array (30, -2, -2));}
      else {$looping = (($encrypt) ? array (0, 32, 2, 62, 30, -2, 64, 96, 2) : array (94, 62, -2, 32, 64, 2, 30, -2, -2));}  //pad the message depending on the padding parameter
      if ($padding == 2) $message .= "        "; //pad the message with spaces
      else if ($padding == 1) {$temp = chr (8-($len%8)); $message .= $temp . $temp . $temp . $temp . $temp . $temp . $temp . $temp;} //PKCS7 padding
      else if (!$padding) $message .= (chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0)); //pad the message out with null bytes  //store the result here
      $result = "";
      $tempresult = "";  if ($mode == 1) { //CBC mode
        $cbcleft = (ord($iv{$m++}) << 24) | (ord($iv{$m++}) << 16) | (ord($iv{$m++}) << 8) | ord($iv{$m++});
        $cbcright = (ord($iv{$m++}) << 24) | (ord($iv{$m++}) << 16) | (ord($iv{$m++}) << 8) | ord($iv{$m++});
        $m=0;
      }  //loop through each 64 bit chunk of the message
      while ($m < $len) {
        $left = (ord($message{$m++}) << 24) | (ord($message{$m++}) << 16) | (ord($message{$m++}) << 8) | ord($message{$m++});
        $right = (ord($message{$m++}) << 24) | (ord($message{$m++}) << 16) | (ord($message{$m++}) << 8) | ord($message{$m++});    //for Cipher Block Chaining mode, xor the message with the previous result
        if ($mode == 1) {if ($encrypt) {$left ^= $cbcleft; $right ^= $cbcright;} else {$cbcleft2 = $cbcleft; $cbcright2 = $cbcright; $cbcleft = $left; $cbcright = $right;}}    //first each 64 but chunk of the message must be permuted according to IP
        $temp = (($left >> 4 & $masks[4]) ^ $right) & 0x0f0f0f0f; $right ^= $temp; $left ^= ($temp << 4);
        $temp = (($left >> 16 & $masks[16]) ^ $right) & 0x0000ffff; $right ^= $temp; $left ^= ($temp << 16);
        $temp = (($right >> 2 & $masks[2]) ^ $left) & 0x33333333; $left ^= $temp; $right ^= ($temp << 2);
        $temp = (($right >> 8 & $masks[8]) ^ $left) & 0x00ff00ff; $left ^= $temp; $right ^= ($temp << 8);
        $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1);    $left = (($left << 1) | ($left >> 31 & $masks[31])); 
        $right = (($right << 1) | ($right >> 31 & $masks[31]));     //do this either 1 or 3 times for each chunk of the message
        for ($j=0; $j<$iterations; $j+=3) {
          $endloop = $looping[$j+1];
          $loopinc = $looping[$j+2];
          //now go through and perform the encryption or decryption  
          for ($i=$looping[$j]; $i!=$endloop; $i+=$loopinc) { //for efficiency
            $right1 = $right ^ $keys[$i]; 
            $right2 = (($right >> 4 & $masks[4]) | ($right << 28 & 0xffffffff)) ^ $keys[$i+1];
            //the result is attained by passing these bytes through the S selection functions
            $temp = $left;
            $left = $right;
            $right = $temp ^ ($spfunction2[($right1 >> 24 & $masks[24]) & 0x3f] | $spfunction4[($right1 >> 16 & $masks[16]) & 0x3f]
                  | $spfunction6[($right1 >>  8 & $masks[8]) & 0x3f] | $spfunction8[$right1 & 0x3f]
                  | $spfunction1[($right2 >> 24 & $masks[24]) & 0x3f] | $spfunction3[($right2 >> 16 & $masks[16]) & 0x3f]
                  | $spfunction5[($right2 >>  8 & $masks[8]) & 0x3f] | $spfunction7[$right2 & 0x3f]);
          }
          $temp = $left; $left = $right; $right = $temp; //unreverse left and right
        } //for either 1 or 3 iterations    //move then each one bit to the right
        $left = (($left >> 1 & $masks[1]) | ($left << 31)); 
        $right = (($right >> 1 & $masks[1]) | ($right << 31));     //now perform IP-1, which is IP in the opposite direction
        $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1);
        $temp = (($right >> 8 & $masks[8]) ^ $left) & 0x00ff00ff; $left ^= $temp; $right ^= ($temp << 8);
        $temp = (($right >> 2 & $masks[2]) ^ $left) & 0x33333333; $left ^= $temp; $right ^= ($temp << 2);
        $temp = (($left >> 16 & $masks[16]) ^ $right) & 0x0000ffff; $right ^= $temp; $left ^= ($temp << 16);
        $temp = (($left >> 4 & $masks[4]) ^ $right) & 0x0f0f0f0f; $right ^= $temp; $left ^= ($temp << 4);    //for Cipher Block Chaining mode, xor the message with the previous result
        if ($mode == 1) {if ($encrypt) {$cbcleft = $left; $cbcright = $right;} else {$left ^= $cbcleft2; $right ^= $cbcright2;}}
        $tempresult .= (chr($left>>24 & $masks[24]) . chr(($left>>16 & $masks[16]) & 0xff) . chr(($left>>8 & $masks[8]) & 0xff) . chr($left & 0xff) . chr($right>>24 & $masks[24]) . chr(($right>>16 & $masks[16]) & 0xff) . chr(($right>>8 & $masks[8]) & 0xff) . chr($right & 0xff));    $chunk += 8;
        if ($chunk == 512) {$result .= $tempresult; $tempresult = ""; $chunk = 0;}
      } //for every 8 characters, or 64 bits in the message  //return the result as an array
      return ($result . $tempresult);
    } //end of des
      

  5.   

    代码太长了,贴不下.需要的去 http://www.mbaiwan.com/mdb/des.php 查看
      

  6.   

    http://www.mbaiwan.com/mbd/des.php  是这个地址,写错了.刚才
      

  7.   

    楼主看看 kemy88的吧,似乎不错