Encrypt and decrypt string function source code in PHP

Source: Internet
Author: User
The source code of the cryptographic decryption string function in PHP:
  1. /**
  2. * Function: Encrypt string processing
  3. * Parameter one: Content that needs to be encrypted
  4. * Parameter two: key
  5. */
  6. function Passport_encrypt ($STR, $key) {//encryption function
  7. Srand (Double) microtime () * 1000000);
  8. $encrypt _key=md5 (rand (0, 32000));
  9. $ctr = 0;
  10. $tmp = ";
  11. for ($i =0; $i<>
  12. $ctr = $ctr ==strlen ($encrypt _key)? 0: $ctr;
  13. $tmp. = $encrypt _key[$ctr]. ($str [$i] ^ $encrypt _key[$ctr + +]);
  14. }
  15. Return Base64_encode (Passport_key ($tmp, $key));
  16. }
  17. /**
  18. * Function: Decryption of strings
  19. * Parameter one: ciphertext to be decrypted
  20. * Parameter two: key
  21. */
  22. function Passport_decrypt ($STR, $key) {//decryption functions
  23. $str =passport_key (Base64_decode ($STR), $key);
  24. $tmp = ";
  25. for ($i =0; $i<>
  26. $MD 5= $str [$i];
  27. $tmp. = $str [+ + $i] ^ $MD 5;
  28. }
  29. return $tmp;
  30. }
  31. /**
  32. * Auxiliary functions
  33. */
  34. function Passport_key ($str, $encrypt _key) {
  35. $encrypt _key=md5 ($encrypt _key);
  36. $ctr = 0;
  37. $tmp = ";
  38. for ($i =0; $i<>
  39. $ctr = $ctr ==strlen ($encrypt _key)? 0: $ctr;
  40. $tmp. = $str [$i] ^ $encrypt _key[$ctr + +];
  41. }
  42. return $tmp;
  43. }
  44. $str = ' Author: uphtm.com;
  45. $key = ' uphtm.com ';
  46. $encrypt =passport_encrypt ($str, $key);
  47. $decrypt =passport_decrypt ($encrypt, $key);
  48. Echo ' original: ', $str. '
    ";
  49. Echo ' ciphertext: ', $encrypt. "
    ";
  50. echo ' translation: ', $decrypt. '
    ";
  51. ?>
Copy Code
Encrypted decryption, PHP
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.