php加密解密功能類

2021-09-07 07:08:32 字數 4484 閱讀 6664

這兩天突發奇想想要用php寫乙個對日常專案加密以及解密的功能,經過努力簡單的封裝了乙個對php**進行加密解密的類,一些思想也是來自於網路,初步測試用著還行,可以實現對指定專案的加密以及解密(只針對本類中加密的解密)過程,在這裡分享給大家,水平比較有限那裡有錯誤還希望指出,共同提高,一下會給大家列出來實現的思想以及原始碼。

加密過程:

讀取原始檔,base64_encode進行加密,利用混排得到的52個大小寫字母作為秘鑰進行替換

$c=strtr(密文,對應待替換的字母,要替換成的字母);

將兩個秘鑰和密文鏈結起來形成要加密檔案的主要內容

最後根據提前寫好的模板格式,分別對base64_decode,strtr,substr幾個命令進行組合,並將組和好的密文放入模板並在此base64_encode加密,

寫入要加密的檔案。

解密過程:

讀取要解密的檔案,截取出以eval開頭的字串,之後層次擷取得到加密模板中生成的密文

base64_decode解密得到解密後的明文

擷取得到的名文,將原始檔形成的 秘鑰+密文那段的字元擷取出來,通過eval執行擷取的字元使密文賦值給預定義的變數($o0o000)

通過執行base64_decode(strtr(substr($o0o000,52*2),substr($o0o000,52,52),substr($o0o000,0,52)));

進行解密,將解密過的內容(明文)寫入檔案。

總結:其實整個加密解密的過程都是互逆的,加密後的php執行是通過eval()這個函式實現的,解密的過程就是加密的逆過程。

[php]view plain

copy

<?php   

/* * @auther:wangyaofeng 

* @time:2014.11.6 

* @action:對php專案進行加密處理 

* @example: 

*  * $ob = new encryption(); 

* $ob->source = "d:\php\www\workspace\weixin1"; 

* $ob->target = "d:\php\www\workspace\weixin2"; 

* $ob->decode('jiami.php'); 

* $ob->decode('dam6.php'); 

* $ob->encodedir($ob->source,$ob->target); 

* $ob->decodedir($ob->target); 

*  * */  

class encryption  

/* *@input  $property_name,$value 

*@output  

*   魔法方法,對變數進行設定值;可根據需求進行處理。直接取出if判斷表示可用設定任何屬性的值,包括不存在的屬性; 

*/  

public function __set($property_name,$value)else  

}  //魔法方法 取出變數的值;  

public function __get($property_name)else  

}  //取隨機排序  

private function randabc($length="")  

//加密處理  

private function ciphertext($filename)  

//生成加密後的模板(簡單版本);  

private function modeleasy()  

//初始化變數  

private function initialvar()  

//生成加密後的模板(複雜版本);  

private function model().$'.$this->q6.'.$'.$this->q6.'.$'.$this->q6.';$'.$this->q3.'=$'.$this->q6.'.$'.$this->q6.'.$'  

.$this->q6.'.$'.$this->q6.'.$'.$this->q6.';$'.$this->q4.'=$'.$this->q3.'.$'.$this->q6.'.$'.$this->q6.'.$'.$this->q3.'  

.$'.$this->q3.'.$'.$this->q6.';$'.$this->q5.'=$'.$this->q6.'.$'.$this->q6.';$'.$this->q1.'.=$'.$this->q6.'.$'.$this->q6.'  

.$'.$this->q6.'.$'.$this->q6.'.$'.$this->q6.'.$'.$this->q6.'.$'.$this->q6.'.$'.$this->q6.'.$'.$this->q6.';  

eval($'.$this->q1.'("'.base64_encode('$'.$this->q2.'="'.$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($file,$target)  

//加密處理 連貫操作  

public function encode($file,$target)  

//解密  

public function decode($file,$target=''))($($($,$*2),$($,$,$),$($,0,$))); 

}else 

//開啟檔案 

$fpp1 = fopen($file,'w'); 

//寫入檔案 

fwrite($fpp1,$s) or die('寫入失敗!');*/  

//讀取要解密的檔案  

$fpp1 = file_get_contents($file);  

//擷取以 eval開頭的部分  

preg_match('[eval.*]', $fpp1,$m);  

if(!isset($m[0]))  

$len = strlen($m[0]);  

//將eval開頭的字串中間的加密字元擷取出來  

$str = substr($m[0],strlen('eval($o00o0o('),$len-18);  

//對密文進行解密  

$str = base64_decode($str);  

//截取出解密後的  核心密文  

$str = substr($str,0 ,strlen($str)-120);  

//執行核心密文  

eval($str);  

//解密  

$s = base64_decode(strtr(substr($o0o000,52*2),substr($o0o000,52,52),substr($o0o000,0,52)));  

//寫入檔案  

$fpp1 = fopen($file,'w');  

fwrite($fpp1,$s) or die('寫入失敗!');  

echo 'decode------'.$target.'-----ok

';  

}  //遞迴解密 對指定資料夾下的php檔案解密  

public function decodedir($source,$target="")  

}  }else if(is_file($source))else  

//return;  

}  }  

//遞迴加密 對指定資料夾下的php檔案加密  

public function encodedir($source,$target)  

}  }else if(is_file($source))else  

//return;  

}  }  

}  $ob = new encryption();  

$ob->source = "d:\php\www\workspace\weixin1";  

$ob->target = "d:\php\www\workspace\weixin2";  

//解密指定檔案  

//$ob->decode('jiami.php');  

//$ob->decode('dam6.php');  

//對乙個指定的檔案目錄進行加密  

//$ob->encodedir($ob->source,$ob->target);  

//對乙個指定的檔案目錄進行解密  

//$ob->decodedir($ob->target);  

PHP加密解密類例項分析

這段 支援 陣列加密 密文有效期,各種對稱加密 其中引數如下 use ption en string,key param string string 需要加密的字串 param string skey 金鑰 param int expiry 密文有效期,加密時候有效,單位 秒,0 為永久有效 ret...

PHP加密解密

加密 function string2secret str 解密 function secret2string sec echo secret2string string2secret 11111111111111111 顯示結果是11111111111111111 echo string2secr...

PHP 加密 解密

由於出於安全考慮,引數傳遞的時候需要進行加密和解密,乙個比較簡單的方法是直接使用php中的函式mcrypt encrypt mcrypt decrypt,乙個加密,乙個解密,但是問題又出現了,這個加密過程中會產生一些使url混亂的符號,於是在加密後對加密字元再進行一次處理,然後多了一一次解析 key...