Home > Backend Development > PHP Tutorial > Refer to the PHP encryption and decryption processing class written by discuz's passport

Refer to the PHP encryption and decryption processing class written by discuz's passport

WBOY
Release: 2016-07-25 09:03:40
Original
956 people have browsed it
  1. /*----------------------------------------- -
  2. = Copyright Agreement:
  3. = GPL (The GNU GENERAL PUBLIC LICENSE Version 2, June 1991)
  4. =--------------------------- ----------------------------------
  5. = File name: cls.sys_crypt.php
  6. = Summary: php encryption and decryption Processing class
  7. = Version: 1.0
  8. = Reference: Discuz forum’s passport related functions
  9. =---------------------------------- --------------------------------
  10. = Script Written By PHPWMS project team
  11. = Last updated: xinge
  12. = Last date: 2007- 12-09
  13. ---------------------------------------------=*/
  14. class SysCrypt {
  15. private $crypt_key;
  16. //Constructor
  17. public function __construct($crypt_key) {
  18. $this -> crypt_key = $crypt_key;
  19. }
  20. public function php_encrypt($txt) {
  21. srand((double) microtime() * 1000000);
  22. $encrypt_key = md5(rand(0,32000));
  23. $ctr = 0;
  24. $tmp = '';
  25. for($i = 0;$i $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  26. $tmp .= $encrypt_key[$ctr].($txt[$i]^$encrypt_key[$ctr++] );
  27. }
  28. return base64_encode(self::__key($tmp,$this -> crypt_key));
  29. }
  30. public function php_decrypt($txt) {
  31. $txt = self::__key(base64_decode($txt ),$this -> crypt_key);
  32. $tmp = '';
  33. for($i = 0;$i < strlen($txt); $i++) {
  34. $md5 = $txt[$i];
  35. $tmp .= $txt[++$i] ^ $md5;
  36. }
  37. return $tmp;
  38. }
  39. private function __key($txt,$encrypt_key) {
  40. $encrypt_key = md5($encrypt_key);
  41. $ctr = 0;
  42. $tmp = '';
  43. for($i = 0; $i < strlen($txt); $i++) {
  44. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  45. $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
  46. }
  47. return $tmp;
  48. }
  49. public function __destruct() {
  50. $this -> crypt_key = null;
  51. }
  52. }
  53. #--Call example
  54. $sc = new SysCrypt('phpwms');
  55. $text = '110';
  56. print($sc -> php_encrypt($text));
  57. print('< ;br>');
  58. print($sc -> php_decrypt($sc -> php_encrypt($text)));
  59. ?>
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template