php 实现php代码的加密解密

php 代码加密类,大家可以根据自己的需求进行修改,原类如下,是对之前的加密解密类的有一次修改,希望能分享给大家。本次在ubuntu下测试没有问题,与之前的版本的区别在于,这次的版本更加的通用性。

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

猜你喜欢

转载自www.cnblogs.com/waw/p/9140082.html