首頁 >後端開發 >php教程 >php加密解密處理類

php加密解密處理類

WBOY
WBOY原創
2016-07-25 08:42:28859瀏覽
  1. class SysCrypt {
  2. private $crypt_key;
  3. // 构造函数
  4. public function __construct($crypt_key) {
  5. $this -> crypt_key = $crypt_key;
  6. }
  7. public function php_encrypt($txt) {
  8. srand((double)microtime() * 1000000);
  9. $encrypt_key = md5(rand(0,32000));
  10. $ctr = 0;
  11. $tmp = '';
  12. for($i = 0;$i $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  13. $tmp .= $encrypt_key[$ctr].($txt[$i]^$encrypt_key[$ctr ]);
  14. }
  15. return base64_encode(self::__key($tmp,$this -> crypt_key));
  16. }
  17. public function php_decrypt($txt) {
  18. $txt = self::__key(base64_decode($txt),$this -> crypt_key);
  19. $tmp = '';
  20. for($i = 0;$i < strlen($txt); $i ) {
  21. $md5 = $txt[$i];
  22. $tmp .= $txt[ $i] ^ $md5;
  23. }
  24. return $tmp;
  25. }
  26. private function __key($txt,$encrypt_key) {
  27. $encrypt_key = md5($encrypt_key);
  28. $ctr = 0;
  29. $tmp = '';
  30. for($i = 0; $i < strlen($txt); $i ) {
  31. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  32. $tmp .= $txt[$i] ^ $encrypt_key[$ctr ];
  33. }
  34. return $tmp;
  35. }
  36. public function __destruct() {
  37. $this -> crypt_key = null;
  38. }
  39. }
  40. $sc = new SysCrypt('phpwms');
  41. $text = '110';
  42. print($sc -> php_encrypt($text));
  43. print('
    ');
  44. print($sc -> php_decrypt($sc -> php_encrypt($text)));
  45. ?>
复制代码

加密解密, php


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn