Home  >  Article  >  Backend Development  >  Encryption and decryption string function source code in PHP

Encryption and decryption string function source code in PHP

WBOY
WBOYOriginal
2016-07-25 08:44:211013browse
PHP中加密解密字符串函数源代码:
  1. /**
  2. *Function: Encrypt string
  3. *Parameter 1: Content to be encrypted
  4. *Parameter 2: Key
  5. */
  6. function passport_encrypt($str,$key){ //加密函数
  7. srand((double)microtime() * 1000000);
  8. $encrypt_key=md5(rand(0, 32000));
  9. $ctr=0;
  10. $tmp='';
  11. for($i=0;$i $ctr=$ctr==strlen($encrypt_key)?0:$ctr;
  12. $tmp.=$encrypt_key[$ctr].($str[$i] ^ $encrypt_key[$ctr++]);
  13. }
  14. return base64_encode(passport_key($tmp,$key));
  15. }
  16. /**
  17. *Function: Decrypt string
  18. *Parameter 1: ciphertext to be decrypted
  19. *Parameter 2: Key
  20. */
  21. function passport_decrypt($str,$key){ //解密函数
  22. $str=passport_key(base64_decode($str),$key);
  23. $tmp='';
  24. for($i=0;$i $md5=$str[$i];
  25. $tmp.=$str[++$i] ^ $md5;
  26. }
  27. return $tmp;
  28. }
  29. /**
  30. *Auxiliary function
  31. */
  32. function passport_key($str,$encrypt_key){
  33. $encrypt_key=md5($encrypt_key);
  34. $ctr=0;
  35. $tmp='';
  36. for($i=0;$i $ctr=$ctr==strlen($encrypt_key)?0:$ctr;
  37. $tmp.=$str[$i] ^ $encrypt_key[$ctr++];
  38. }
  39. return $tmp;
  40. }
  41. $str='作者:uphtm.com;
  42. $key='uphtm.com';
  43. $encrypt=passport_encrypt($str,$key);
  44. $decrypt=passport_decrypt($encrypt,$key);
  45. echo '原文:',$str."

    ";
  46. echo '密文:',$encrypt."

    ";
  47. echo '译文:',$decrypt."

    ";
  48. ?>
复制代码

加密解密, PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn