当前位置:Gxlcms > PHP教程 > 参考discuz的passpor写的php加密解密处理类

参考discuz的passpor写的php加密解密处理类

时间:2021-07-01 10:21:17 帮助过:5人阅读

  1. /*---------------------------------------
  2. = 版权协议:
  3. = GPL (The GNU GENERAL PUBLIC LICENSE Version 2, June 1991)
  4. =------------------------------------------------------------
  5. = 文件名称:cls.sys_crypt.php
  6. = 摘 要:php加密解密处理类
  7. = 版 本:1.0
  8. = 参 考:Discuz论坛的passport相关函数
  9. =------------------------------------------------------------
  10. = Script Written By PHPWMS项目组
  11. = 最后更新:xinge
  12. = 最后日期:2007-12-09
  13. ---------------------------------------=*/
  14. class SysCrypt {
  15. private $crypt_key;
  16. // 构造函数
  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. #--调用示例
  54. $sc = new SysCrypt('phpwms');
  55. $text = '110';
  56. print($sc -> php_encrypt($text));
  57. print('
    ');
  58. print($sc -> php_decrypt($sc -> php_encrypt($text)));
  59. ?>

人气教程排行