php 程式碼加密類別
- /*
- * @auther:wangyaofeng
- * @time:2014.11.6
- * @action:對php專案進行加密處理,注意如果專案中存在框架目錄或沒有必要加密的目錄,請提前移出
- * */
- class Encryption{
- private $c='';//儲存密文
- private $s='',$q1,$q2,$q3,$q4,$q5,$q6;//儲存產生的加密後的檔案內容
- //如果不設定一個值,isset會表示不存在;
- private $file='';//讀取檔案的路徑
- private $source='',$target='';
- //建構函數,實例化時調用初始化全局變量;
- public function __construct(){
- //初始化全局變量
- $this->initialVar();
- //echo "hello n";
- }
- /*
- *@input $property_name,$value
- *@output
- * 魔法方法,設定變數值;可依需求處理。若直接移除if判斷表示可用設定任何屬性的值,包含不存在的屬性;
- */
- public function __set($property_name,$value){
- //定義過的變數;
- if(isset($this->$property_name)){
- $this->$property_name = $value;
- }else{
- //異常處理,處理未宣告的變數賦值;可依需求進行處理。
- throw new Exception("property does not exist");
- }
- }
- //魔法方法取出變數的值;
- public function __get($property_name){
- if🎜> if (isset($this->$property_name)){
- return $this->$property_name;
- }else{
- //throw new Exception("property does not exist");
- return元;
- }
- }
- //取隨機排序
- private function RandAbc($length=""){//隨機排序取回
- $str="ABCDE > return str_shuffle($str);
- }
- //對明文內容加密處理
- private function ciphertext($filename){
- //$filename='index.php';
- $T_k1=$this->RandAbc();
- $T_k2=$this->RandAbc();
- $vstr=file_get_contents($filename);
- $v1=base64_encode($vstr);
- $c=strtr($v1,$T_k1,$T_k2);
- $this->c=$T_k1.$T_k2.$c;
- return $this;
- }
- //初始化變數
- private function initialVar(){
- $this->q1="O00O0O";//base64_decode
- $this->q2="O0O000";//$c(原文經過strtr置換後的密文,由目標字元取代字元base64_encode('原文內容')構成)
- $this->q3="O0OO00";//strtr
- $this->q4="OO0O00";/ /substr
- $this->q5="OO0000";//52
- $this->q6="O00OO0";//urldecode解析過的字串(n1zb/ma5vt0i28-pxuqy*6workdg9_ehcs
- }
- //產生加密後的模板(複雜版本);
- private function model(){
- //$c = $this->c;
- //$ this->initialVar();
- $this->s='q6.'=urldecode("n1zb/ma5vt0i28-pxuqy*6lrkdg9_ehcswo4+f37j");$'. 🎜> $this->q1.'=$'.$this->q6.'{3}.$'.$this->q6.'{6}.$'.$this->q6.'{33 }.$'.$this->q6.'{30};$'.$this->q3.'=$'.$this->q6.'{33}.$'.$this->q6. '{10}.$'
- .$this->q6.'{24}.$'.$this->q6.'{10}.$'.$this->q6.'{24}; $'.$this->q4.'=$'.$this->q3.'{0}.$'.$this->q6.'{18}.$'.$this->q6.'{ 3}.$'.$this->q3.'{0}
- .$'.$this->q3.'小貝.$'.$this->q6.'{24};$'. $this->q5.'=$'.$this->q6.'{7}.$'.$this->q6.'{13};$'.$this->q1.'.=$' .$this->q6.'{22}.$'.$this->q6.'{36}
- .$'.$this->q6.'{29}.$'.$this-> q6.'{26}.$'.$this->q6.'{30}.$'.$this->q6.'{32}.$'.$this->q6.'{35}.$ '.$this->q6.'{26}.$'.$this->q6.'{30};
- eval($'.$this->q1.'("'.base64_encode('$ '.$this->q2.'="'.$this->c.'";
- eval('?>'.$'.$this->q1.'($'.$this-> q3.'($'.$this->q4.'($'.$this->q2.',$'.$this->q5.'*2),$'.$this->q4.' ($'.$this->q2.',$'.$this->q5.',$'.$this->q5.'),
- $'.$this->q4.'($ '.$this->q2.',0,$'.$this->q5.'))));').'"));?>';
- return $this;
- }
- //建立加密檔案
- private function build($target){
- //$this->encodes("./index.php");
- //$this->model( );
- $fpp1 = fopen($target,'w');
- fwrite($fpp1,$this->s) or die('寫入是失敗!');
- fclose($fpp1);
- return $this;
- }
- //加密處理連貫操作
- public function encode($file,$target){
- / /$file = "index.php";
- //連貫操作其實就是利用函數處理完後返回自身
- $this->ciphertext($file)->model()->build($target) ;
- echo 'encode------'.$target.'-----ok
';
- }
- //解密
- public function decode($file ,$target=''){
- //讀取要解密的檔案
- $fpp1 = file_get_contents($file);
- $this->decodeMode($fpp1)->build($target) ;
- echo 'decode------'.$target.'-----ok
';
- }
- //解密模板,得到解密後的文字
- private function decodeMode($fpp1){
- //以eval為標誌截取為數組,前半部為密文中的替換掉的函數名,後半部為密文
- $m = explode('eval ',$fpp1);
- //對系統函數的替換部分進行執行,得到系統變數
- $varStr = substr($m[0],strpos($m[0],'$')) ;
- //執行後,後續就可以使用替換後的系統函式名稱
- eval($varStr);
- //判斷是否有密文
- if(!isset($m[1 ])){
- return $this;
- }
-
- //對密文截取{$this->q4} substr
- $star = strripos($m[1], '(');
- $end = strpos($m[1],')');
- $str = ${$this->q4}($m[1],$star,$end );
- //對密文解密{$this->q1} base64_decode
- $str = ${$this->q1}($str);
- //截取出解密後的核心密文
- $evallen = strpos($str,'eval');
- $str = substr($str,0,$evallen);
- //執行核心密文使系統變數被賦予值$ O0O000
- eval($str);
- //並不能將如下段封裝,因為${$this->qn} 並不能在全文中起作用
- $this->s = ${$ this->q1}(
- ${$this->q3}(
- ${$this->q4}(
- ${$this->q2},${$this->q5} *2
- ),
- ${$this->q4}(
- ${$this->q2},${$this->q5},${$this->q5}
- ),
- ${$this->q4}(
- ${$this->q2},0,${$this->q5}
- )
- )
- );
- return $this;
- }
- //遞歸讀取並建立目標目錄結構
- private function targetDir($target){
- if(!empty($target) ) {
- if(!file_exists($target)){
- mkdir($target,0777,true);
- }else{
- chmod($target,0777);
- }
-
-
- }
- }
-
- //遞歸解密對指定資料夾下的php檔案解密
- public function decodeDir($source,$target=""){
- if(is_dir($ source)){
- $this->targetDir($target);
- $dir = opendir($source);
- while(false!=$file=readdir($dir))
- {
- //列出所有檔案並去掉'.'和'..' 此處使用的實例為thinkphp框架,所以預設排除裡Thinkphp目錄,使用者可以依照自己的需求設定
- if($file! ='.' && $file!='..' && $file !='ThinkPHP')
- {
- $path = $target.DIRECTORY_SEPARATOR.$file;
- $sourcePath = $source.DIRECTORY_SEPARATOR.$file;
- $sourcePath = $source.DIRECTORY_SEPARATOR .$file;
- $this->decodeDir($sourcePath,$path);
- }
- }
-
- }else if(is_file($source)){
- $extension =substr($source,strrpos($source,'.') 1);
- if(strtolower($extension)=='php'){
- $this->decode($source,$target) ;
- }else{
- //不是php的檔案不處理
- copy($source, $target);
- }
- //return; } }
- //遞歸加密對指定資料夾下的php檔案加密
- public function encodeDir($source,$target){
- if(is_dir($source)){
- $this->targetDir ($target);
- $dir = opendir($source);
- while(false!=$file=readdir($dir))
- {
- //列出所有檔案並去掉' .'和'..'
- if($file!='.' && $file!='..' && $file !='ThinkPHP')
- {
- $path = $target. DIRECTORY_SEPARATOR.$file;
- $sourcePath = $source.DIRECTORY_SEPARATOR.$file;
- $this->encodeDir($sourcePath,$path);
- }
- }
-
- else if(is_file($source)){
- $extension=substr($source,strrpos($source,'.') 1);
- if(strtolower($extension)=='php'){
- $this->encode($source,$target);
- }else{
- copy($source, $target);
- }
- }
- }
- }
- $ob = new Encryption();
- $ob->source = "/var/www/bookReservation";
- $ob->target = "/var/www/jiami/bookReservation";
- //解密指定檔案
- //$ob->decode('D:\php\WWW\workspace\weixin2\Application\Home\Controller\IndexController.class.php');
-
- //$ob->decode('jiami.php');
- //$ob->decode('dam6.php');
- //對一個指定的檔案目錄進行加密
- $ ob->encodeDir($ob->source,$ob->target);
- //對一個指定的檔案目錄進行解密
- $ob->decodeDir($ob->target,"/var/ www/jiami/bookReservationD");
複製程式碼
|