欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-04-23 11:31:31
...
  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 $md5 = $txt[$i];
  34. $tmp .= $txt[++$i] ^ $md5;
  35. }
  36. return $tmp;
  37. }
  38. private function __key($txt,$encrypt_key) {
  39. $encrypt_key = md5($encrypt_key);
  40. $ctr = 0;
  41. $tmp = '';
  42. for($i = 0; $i $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  43. $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
  44. }
  45. return $tmp;
  46. }
  47. public function __destruct() {
  48. $this -> crypt_key = null;
  49. }
  50. }
  51. #--调用示例
  52. $sc = new SysCrypt('phpwms');
  53. $text = '110';
  54. print($sc -> php_encrypt($text));
  55. print('
    ');
  56. print($sc -> php_decrypt($sc -> php_encrypt($text)));
  57. ?>
复制代码