• 2007-07-02

    PHP 写的加密函数,支持私人密钥 - [网页制作]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://chaochao.blogbus.com/logs/6272869.html

    1. <?php
    2. // 说明:PHP 写的加密函数,支持私人密钥
    3. function keyED($txt,$encrypt_key)  
    4. {  
    5.     $encrypt_key = md5($encrypt_key);  
    6.     $ctr=0;  
    7.     $tmp = "";  
    8.     for ($i=0;$i<strlen($txt);$i++)  
    9.     {  
    10.         if ($ctr==strlen($encrypt_key)) $ctr=0;  
    11.         $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);  
    12.         $ctr++;  
    13.     }  
    14.     return $tmp;  
    15. }  
    16. function encrypt($txt,$key)  
    17. {  
    18.     srand((double)microtime()*1000000);  
    19.     $encrypt_key = md5(rand(0,32000));  
    20.     $ctr=0;  
    21.     $tmp = "";  
    22.     for ($i=0;$i<strlen($txt);$i++)  
    23.     {  
    24.         if ($ctr==strlen($encrypt_key)) $ctr=0;  
    25.         $tmp.= substr($encrypt_key,$ctr,1) . (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));  
    26.         $ctr++;  
    27.     }  
    28.     return keyED($tmp,$key);
    29. }  
    30. function decrypt($txt,$key)  
    31. {  
    32.     $txt = keyED($txt,$key);  
    33.     $tmp = "";  
    34.     for ($i=0;$i<strlen($txt);$i++)  
    35.     {  
    36.         $md5 = substr($txt,$i,1);  
    37.         $i++;  
    38.         $tmp.= (substr($txt,$i,1) ^ $md5);  
    39.     }  
    40.     return $tmp;  
    41. } 
    42. $string = "我是加密字符";  
    43. // encrypt $string, and store it in $enc_text  
    44. $enc_text = encrypt($string,$key);  
    45. // decrypt the encrypted text $enc_text, and store it in $dec_text  
    46. $dec_text = decrypt($enc_text,$key);  
    47. print "加密的 text : $enc_text <Br> ";  
    48. print "解密的 text : $dec_text <Br> ";  
    49. ?>

    历史上的今天:


    收藏到:Del.icio.us