欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

强大的PHP加密解密类

程序员文章站 2022-06-03 09:54:05
...
强大的PHP加密解密类 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. }
复制代码