參考discuz的passpor寫的php加密解密處理類

來源:互聯網
上載者:User
  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. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.