当前位置:Gxlcms > PHP教程 > 强大的PHP加密解密类

强大的PHP加密解密类

时间:2021-07-01 10:21:17 帮助过:18人阅读

填写您的邮件地址,订阅我们的精彩内容: http://blog.ddian.cn/?post=923
  1. class Ender{
  2. private $enkey;//加密解密用的密钥
  3. //构造参数是密钥
  4. public function __construct($key=''){
  5. if(!$key){
  6. $this->enkey=$key;
  7. }
  8. }
  9. //设置密钥
  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. //加密字符串
  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. //解密字符串
  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. }

人气教程排行