当前位置:首页 > PHP教程 > php高级应用 > 列表

用php实现php代码的加密解密类分享

发布:smiling 来源: PHP粉丝网  添加日期:2018-10-13 14:14:05 浏览: 评论:0 

php 代码加密类,大家可以根据自己的需求进行修改,原类如下,希望能分享给大家。本次在ubuntu下测试没有问题。

  1. <?php   
  2.     class Encryption{   
  3.         private $c='';//存储密文   
  4.         private $s='',$q1,$q2,$q3,$q4,$q5,$q6;//存储生成的加密后的文件内容   
  5.         //如果不设置一个值,isset会表示不存在;   
  6.         private $file='';//读取文件的路径   
  7.         private $source='',$target=''
  8.         //构造函数,实例化时调用初始化全局变量;   
  9.         public function __construct(){   
  10.             //初始化全局变量   
  11.             $this->initialVar();   
  12.             //echo "hello \n";   
  13.         }   
  14.         /*  
  15.         *@input  $property_name,$value  
  16.         *@output   
  17.         *   魔法方法,对变量进行设置值;可根据需求进行处理。若直接去除if判断表示可用设置任何属性的值,包括不存在的属性; 
  18.         */   
  19.         public function __set($property_name,$value){   
  20.             //定义过的变量;   
  21.             if(isset($this->$property_name)){   
  22.                 $this->$property_name = $value;   
  23.             }else{   
  24.                 //异常处理,处理未声明的变量赋值;可根据需求进行处理。   
  25.                 throw new Exception("property does not exist");   
  26.             }   
  27.         }   
  28.         //魔法方法 取出变量的值;   
  29.         public function __get($property_name){   
  30.             if(isset($this->$property_name)){   
  31.                 return $this->$property_name;   
  32.             }else{   
  33.                 //throw new Exception("property does not exist");   
  34.                 return NULL;   
  35.             }   
  36.         }   
  37.         //取随机排序   
  38.         private function RandAbc($length=""){//随机排序取回   
  39.           $str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";   
  40.           return str_shuffle($str);   
  41.         }   
  42.         //对明文内容进行加密处理 
  43.         private function ciphertext($filename){   
  44.             //$filename='index.php';   
  45.             $T_k1=$this->RandAbc();   
  46.             $T_k2=$this->RandAbc();   
  47.             $vstr=file_get_contents($filename);   
  48.             $v1=base64_encode($vstr);   
  49.             $c=strtr($v1,$T_k1,$T_k2); 
  50.             $this->c=$T_k1.$T_k2.$c;   
  51.             return $this;   
  52.         } 
  53.         //初始化变量   
  54.         private function initialVar(){   
  55.             $this->q1="O00O0O";//base64_decode   
  56.             $this->q2="O0O000";//$c(原文经过strtr置换后的密文,由 目标字符+替换字符+base64_encode(‘原文内容’)构成)   
  57.             $this->q3="O0OO00";//strtr   
  58.             $this->q4="OO0O00";//substr   
  59.             $this->q5="OO0000";//52   
  60.             $this->q6="O00OO0";//urldecode解析过的字符串(n1zb/ma5\vt0i28-pxuqy*6%6Crkdg9_ehcswo4+f37j) 
  61.  
  62.         }   
  63.         //生成加密后的模板(复杂版本);   
  64.         private function model(){   
  65.             //$c = $this->c; 
  66.             //$this->initialVar();   
  67.             $this->s='<?php $'.$this->q6.'=urldecode("%6E1%7A%62%2F%6D%615%5C%76%740%6928%2D%70%78%75%71%79%2A6%6C%72%6B%64%679%5F%65%68%63%73%77%6F4%2B%6637%6A");$'.   
  68.             $this->q1.'=$'.$this->q6.'{3}.$'.$this->q6.'{6}.$'.$this->q6.'{33}.$'.$this->q6.'{30};$'.$this->q3.'=$'.$this->q6.'{33}.$'.$this->q6.'{10}.$'   
  69.             .$this->q6.'{24}.$'.$this->q6.'{10}.$'.$this->q6.'{24};$'.$this->q4.'=$'.$this->q3.'{0}.$'.$this->q6.'{18}.$'.$this->q6.'{3}.$'.$this->q3.'{0}   
  70.             .$'.$this->q3.'{1}.$'.$this->q6.'{24};$'.$this->q5.'=$'.$this->q6.'{7}.$'.$this->q6.'{13};$'.$this->q1.'.=$'.$this->q6.'{22}.$'.$this->q6.'{36}   
  71.             .$'.$this->q6.'{29}.$'.$this->q6.'{26}.$'.$this->q6.'{30}.$'.$this->q6.'{32}.$'.$this->q6.'{35}.$'.$this->q6.'{26}.$'.$this->q6.'{30};   
  72.             eval($'.$this->q1.'("'.base64_encode('$'.$this->q2.'="'.$this->c.'"; 
  73.             eval(\'?>\'.$'.$this->q1.'($'.$this->q3.'($'.$this->q4.'($'.$this->q2.',$'.$this->q5.'*2),$'.$this->q4.'($'.$this->q2.',$'.$this->q5.',$'.$this->q5.'),   
  74.             $'.$this->q4.'($'.$this->q2.',0,$'.$this->q5.'))));').'"));?>';   
  75.             return $this;   
  76.         } 
  77.         //创建加密文件   
  78.         private function build($target){ 
  79.             //$this->encodes("./index.php");   
  80.             //$this->model();   
  81.             $fpp1 = fopen($target,'w');   
  82.             fwrite($fpp1,$this->s) or die('写入是失败!'); 
  83.             fclose($fpp1); 
  84.             return $this;   
  85.         }   
  86.         //加密处理 连贯操作   
  87.         public function encode($file,$target){   
  88.             //$file = "index.php";   
  89.             //连贯操作其实就是利用函数处理完后返回自身   
  90.             $this->ciphertext($file)->model()->build($target); 
  91.             echo 'encode------'.$target.'-----ok<br/>';   
  92.         }   
  93.         //解密   
  94.         public function decode($file,$target=''){ 
  95.             //读取要解密的文件 
  96.             $fpp1 = file_get_contents($file); 
  97.             $this->decodeMode($fpp1)->build($target); 
  98.             echo 'decode------'.$target.'-----ok<br/>'
  99.         } 
  100.         //解密模板,得到解密后的文本 
  101.         private function decodeMode($fpp1){ 
  102.             //以eval为标志 截取为数组,前半部分为密文中的替换掉的函数名,后半部分为密文 
  103.             $m = explode('eval',$fpp1); 
  104.             //对系统函数的替换部分进行执行,得到系统变量 
  105.             $varStr = substr($m[0],strpos($m[0],'$')); 
  106.             //执行后,后续就可以使用替换后的系统函数名 
  107.             eval($varStr); 
  108.             //判断是否有密文 
  109.             if(!isset($m[1])){ 
  110.                 return $this
  111.             } 
  112.  
  113.             //对密文进行截取 {$this->q4}  substr 
  114.             $star =  strripos($m[1],'('); 
  115.             $end = strpos($m[1],')'); 
  116.             $str = ${$this->q4}($m[1],$star,$end); 
  117.             //对密文解密 {$this->q1}  base64_decode 
  118.             $str = ${$this->q1}($str); 
  119.             //截取出解密后的  核心密文 
  120.             $evallen = strpos($str,'eval'); 
  121.             $str = substr($str,0,$evallen); 
  122.             //执行核心密文 使系统变量被赋予值 $O0O000 
  123.             eval($str); 
  124.             //并不能将如下段封装,因为 ${$this->qn} 并不能在全文中起作用 
  125.             $this->s = ${$this->q1}( 
  126.                 ${$this->q3}( 
  127.                     ${$this->q4}( 
  128.                         ${$this->q2},${$this->q5}*2 
  129.                     ), 
  130.                     ${$this->q4}( 
  131.                         ${$this->q2},${$this->q5},${$this->q5} 
  132.                     ), 
  133.                     ${$this->q4}( 
  134.                         ${$this->q2},0,${$this->q5} 
  135.                     ) 
  136.                 ) 
  137.             ); 
  138.             return $this
  139.         } 
  140.         //递归读取并创建目标目录结构 
  141.         private function targetDir($target){ 
  142.             if(!emptyempty($target) )  { 
  143.                 if(!file_exists($target)){ 
  144.                     mkdir($target,0777,true); 
  145.                 }else
  146.                     chmod($target,0777); 
  147.                 } 
  148.  
  149.             } 
  150.         } 
  151.  
  152.         //递归解密 对指定文件夹下的php文件解密   
  153.         public function decodeDir($source,$target=""){ 
  154.             if(is_dir($source)){ 
  155.                 $this->targetDir($target); 
  156.                 $dir = opendir($source);   
  157.                 while(false!=$file=readdir($dir))   
  158.                 {   
  159.                     //列出所有文件并去掉'.'和'..' 此处用的实例为thinkphp框架,所以默认排除里Thinkphp目录,用户可以按照自己的需求设置 
  160.                     if($file!='.' && $file!='..' && $file !='ThinkPHP'
  161.                     {   
  162.                         $path = $target.DIRECTORY_SEPARATOR.$file;   
  163.                         $sourcePath =  $source.DIRECTORY_SEPARATOR.$file;   
  164.                         $this->decodeDir($sourcePath,$path);   
  165.                     }   
  166.                 }   
  167.            
  168.             }else if(is_file($source)){   
  169.                 $extension=substr($source,strrpos($source,'.')+1);   
  170.                 if(strtolower($extension)=='php'){ 
  171.                     $this->decode($source,$target);   
  172.                 }else{   
  173.                     //不是php的文件不处理   
  174.                     copy($source$target);   
  175.                 }   
  176.                 //return;   
  177.             } 
  178.         }   
  179.         //递归加密 对指定文件夹下的php文件加密   
  180.         public function encodeDir($source,$target){ 
  181.             if(is_dir($source)){ 
  182.                 $this->targetDir($target); 
  183.                 $dir = opendir($source);   
  184.                 while(false!=$file=readdir($dir))   
  185.                 {   
  186.                     //列出所有文件并去掉'.'和'..' 
  187.                     if($file!='.' && $file!='..' && $file !='ThinkPHP'
  188.                     {   
  189.                         $path = $target.DIRECTORY_SEPARATOR.$file;   
  190.                         $sourcePath =  $source.DIRECTORY_SEPARATOR.$file;   
  191.                         $this->encodeDir($sourcePath,$path);   
  192.                     }   
  193.                 }   
  194.  
  195.             }else if(is_file($source)){ 
  196.                 $extension=substr($source,strrpos($source,'.')+1); 
  197.                 if(strtolower($extension)=='php'){ 
  198.                     $this->encode($source,$target); 
  199.                 }else{   
  200.                     copy($source$target);   
  201.                 } 
  202.             }   
  203.         } 
  204.     }   
  205.     $ob = new Encryption();   
  206.     $ob->source = "/var/www/bookReservation"
  207.     $ob->target = "/var/www/jiami/bookReservation"
  208.     //解密指定文件   
  209.     //$ob->decode('D:\\php\\WWW\\workspace\\weixin2\\Application\\Home\\Controller\\IndexController.class.php');   
  210.      //phpfensi.com 
  211.     //$ob->decode('jiami.php');   
  212.     //$ob->decode('dam6.php');   
  213.     //对一个指定的文件目录进行加密   
  214.     $ob->encodeDir($ob->source,$ob->target); 
  215.     //对一个指定的文件目录进行解密 
  216.     $ob->decodeDir($ob->target,"/var/www/jiami/bookReservationD"); 

Tags: php加密 php解密

分享到: