Home  >  Article  >  Backend Development  >  php encryption and decryption processing class

php encryption and decryption processing class

WBOY
WBOYOriginal
2016-07-25 08:42:28813browse
  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


Statement:
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