function   passport_decrypt($txt,$key)

   $txt = passport_key(base64_decode($txt), $key);
   $tmp = ''; 
   for ($i = 0;  $i < strlen($txt); $i++) 
   {
   $tmp .=  $txt[$i] ^   $txt[++$i];
   }    
   return   $tmp; 
} function   passport_key($txt,   $encrypt_key) 
{
$encrypt_key   =   md5($encrypt_key); $ctr   =   0; $tmp   =   ''; for($i   =   0;   $i   <   strlen($txt);   $i++)

    $ctr   =   $ctr   ==   strlen($encrypt_key)   ?   0   :   $ctr; $tmp   .=   $txt[$i]   ^   $encrypt_key[$ctr++]; 

return   $tmp; 
}
调用时:
define("value",passport_decrypt($CONFIGROW["txt"],$CONFIGROW["key"]));现在我想保存数据时进行加密,读取时安照上面的解密
谁能帮我写一个加密的方法?
不要直接copy网上的代码....

解决方案 »

  1.   

    function passport_encryt($txt,$key)
    {
      $tmp = '';
      for ($i = 0; $i < strlen($txt); $i++)  
      {
    $tmp .= $txt[$i] . chr(0);
      }  
      return base64_encode(passport_key($tmp,$key));}function passport_decrypt($txt,$key)
    {  
      $txt = passport_key(base64_decode($txt), $key);  $tmp = '';  
      for ($i = 0; $i < strlen($txt); $i++)  
      {
    $tmp .= $txt[$i] ^ $txt[++$i];
      }   
      return $tmp;  
    }  function passport_key($txt, $encrypt_key)  
    {
    $encrypt_key = md5($encrypt_key); 
    $ctr = 0; 
    $tmp = ''; 
    for($i = 0; $i < strlen($txt); $i++)
    {  
    $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr; 
    $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];  
    }  
    return $tmp;  
    }
    $data = "testdata"; 
    $key  = "cipher";
    $o   =  passport_encryt($data,$key);
    $str  =  passport_decrypt($o,$key);
    echo $str;
      

  2.   

    //上面的实现有点傻
    function passport_encryt($txt,$key)
    {
      $tmp = '';
      $r   = md5(uniqid(rand(), true));
      for ($i = 0,$j=strlen($txt); $i < $j; $i++)  
      {
    $tmp .= ($txt[$i] ^ $r[32%$i]) . $r[32%$i]; //经过decryt,就会变成$txt[$i] ^ $r[32%$i] ^ $r[32%$i],显然结果就是原值$txt[$i]
      }  
      return base64_encode(passport_key($tmp,$key));}
      

  3.   

    function   passport_encode($array)  //加密

        $arrayenc   =   array(); 
    foreach($array   as   $key   =>   $val)  
    {
    $arrayenc[]   =   $key.'='.urlencode($val);

    return   implode('&',   $arrayenc); 
    }function   passport_decrypt($txt,$key)    //解密

       $txt = passport_key(base64_decode($txt), $key);
       $tmp = ''; 
       for ($i = 0;  $i < strlen($txt); $i++) 
       {
       $tmp .=  $txt[$i] ^   $txt[++$i];
       }    
       return   $tmp; 

    function   passport_key($txt,   $encrypt_key)  
    {
    $encrypt_key   =   md5($encrypt_key); $ctr   =   0; $tmp   =   ''; for($i   =   0;   $i   <   strlen($txt);   $i++)

        $ctr   =   $ctr   ==   strlen($encrypt_key)   ?   0   :   $ctr; $tmp   .=   $txt[$i]   ^   $encrypt_key[$ctr++]; 

    return   $tmp; 
    }
    在本地测试,这个加密解密都可以,但是我加密的是中文的话,在空间上显示出来是乱码,还不知道原因