您的位置主页 > 编程专区 > Php > php对文本加密解密

php对文本加密解密

2011-02-16    文章来源:互联网    浏览次数:1036     分享文章
 
  1. $key = "This is supposed to be a secret key !!!"
  2. function keyED($txt,$encrypt_key
  3. $encrypt_key = md5($encrypt_key); 
  4. $ctr=0; 
  5. $tmp = ""
  6. for ($i=0;$i<strlen($txt);$i++) 
  7. if ($ctr==strlen($encrypt_key)) $ctr=0; 
  8. $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1); 
  9. $ctr++; 
  10. return $tmp
  11. }
 
  1. function encrypt($txt,$key
  2. srand((double)microtime()*1000000); 
  3. $encrypt_key = md5(rand(0,32000)); 
  4. $ctr=0; 
  5. $tmp = ""
  6. for ($i=0;$i<strlen($txt);$i++) 
  7. if ($ctr==strlen($encrypt_key)) $ctr=0; 
  8. $tmp.= substr($encrypt_key,$ctr,1).(substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1)); 
  9. $ctr++; 
  10. return keyED($tmp,$key); 
  11.  
  12. function decrypt($txt,$key
  13. $txt = keyED($txt,$key); 
  14. $tmp = ""
  15. for ($i=0;$i<strlen($txt);$i++) 
  16. $md5 = substr($txt,$i,1); 
  17. $i++; 
  18. $tmp.= (substr($txt,$i,1) ^ $md5); 
  19. return $tmp
  20.  
  21. $string = "Hello World !!!"
  22. $enc_text = encrypt($string,$key); 
  23. $dec_text = decrypt($enc_text,$key); 
  24.  
  25. print "Original text : $string <Br>"
  26. print "Encrypted text : $enc_text <Br>"
  27. print "Decrypted text : $dec_text <Br>";

 

文章评论(查看全部)

昵 称 *
电子邮箱 *
网 址      0 + 9 = ?