Home >Backend Development >PHP Tutorial >Powerful PHP encryption and decryption class

Powerful PHP encryption and decryption class

WBOY
WBOYOriginal
2016-07-25 09:01:03942browse
Powerful PHP encryption and decryption class http://blog.ddian.cn/?post=923
  1. class Ender{
  2. private $enkey;//Key used for encryption and decryption
  3. //The construction parameter is the key
  4. public function __construct($key=''){
  5. if(!$ key){
  6. $this->enkey=$key;
  7. }
  8. }
  9. //Set the key
  10. public function set_key($key){
  11. $this->enkey=$key;
  12. }
  13. private function keyED ($txt,$encrypt_key)
  14. {
  15. $encrypt_key = md5($encrypt_key);
  16. $ctr=0;
  17. $tmp = "";
  18. for ($i=0;$i {
  19. if ($ctr==strlen($encrypt_key)) $ctr=0;
  20. $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
  21. $ctr++;
  22. }
  23. return $tmp;
  24. }
  25. //Encrypt string
  26. public function encrypt($txt,$key='')
  27. {
  28. if(!$key){
  29. $key=$this-> ;enkey;
  30. }
  31. srand((double)microtime()*1000000);
  32. $encrypt_key = md5(rand(0,32000));
  33. $ctr=0;
  34. $tmp = "";
  35. for ($i =0;$i {
  36. if ($ctr==strlen($encrypt_key)) $ctr=0;
  37. $tmp.= substr($encrypt_key,$ctr,1) .
  38. (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
  39. $ctr++;
  40. }
  41. return base64_encode($this->keyED($tmp,$key)) ;
  42. }
  43. //Decrypt string
  44. public function decrypt($txt,$key='')
  45. {
  46. $txt=base64_decode($txt);
  47. if(!$key){
  48. $key=$this- >enkey;
  49. }
  50. $txt = $this->keyED($txt,$key);
  51. $tmp = "";
  52. for ($i=0;$i {
  53. $md5 = substr($txt,$i,1);
  54. $i++;
  55. $tmp.= (substr($txt,$i,1) ^ $md5);
  56. }
  57. return $tmp; //http://blog.ddian.cn
  58. }
  59. }
Copy code


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