這兩天突發奇想想要用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
/*
* @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('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);
* */
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){
return $this->$property_name;
//throw new Exception("property does not exist");
return NULL;
//取随機排序
private function RandAbc($length=""){//随機排序取回
$str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
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 為錯誤寫法 會報錯Cannot access empty property
//正确寫法 $this->c;
$this->c=$T_k1.$T_k2.$c;
return $this;
//生成加密後的模闆(簡單版本);
private function modelEasy(){
$this->s='<?php
'.'$c='
.'"'.$this->c.'";'."\n".
'$m='
.
'strtr(substr($c,52*2),substr($c,52,52),substr($c,0,52));'."\n"
'$s=base64_decode('
'$m'
');'.
'eval("'.'?>".$s'.');'.
'
?>';
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/ma5\vt0i28-pxuqy*6%6Crkdg9_ehcswo4+f37j)
//生成加密後的模闆(複雜版本);
private function model(){
$c = $this->c;
//$this->initialVar();
$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");$'.
$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.'{1}.$'.$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.'="'.$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.'))));').'"));?>';
//建立加密檔案
private function build($file,$target){
//$this->encodes("./index.php");
//$this->model();
$fpp1 = fopen($target,'w');
fwrite($fpp1,$this->s) or die('寫入是失敗!');
//加密處理 連貫操作
public function encode($file,$target){
//$file = "index.php";
//連貫操作其實就是利用函數處理完後傳回自身
$this->ciphertext($file)->model()->build($file,$target);
echo 'encode------'.$target.'-----ok<br/>';
//解密
public function decode($file,$target=''){
//導入檔案(類名不應該和導入檔案的類名相同)
//$file = ($target=='')?$file:$target;
/*require_once("$file");
//寫入檔案中的解析後的值(簡單版的解密直接是$s)
//fwrite($fpp1,$s) or die('寫入失敗!');
//初始化系統常量
//$this->initialVar();
//
if(${$this->q2}){
$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})));
}else{
$s = file_get_contents($file);
}
//打開檔案
$fpp1 = fopen($file,'w');
//寫入檔案
fwrite($fpp1,$s) or die('寫入失敗!');*/
//讀取要解密的檔案
$fpp1 = file_get_contents($file);
//截取以 eval開頭的部分
preg_match('[eval.*]', $fpp1,$m);
if(!isset($m[0])){
return;
$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<br/>';
//遞歸解密 對指定檔案夾下的php檔案解密
public function decodeDir($source,$target=""){
//$target = $source;
if(is_dir($source)){
//mkdir($target);
$dir = opendir($source);
while(false!=$file=readdir($dir))
{
//列出所有檔案并去掉'.'和'..' Thinkphp 表示項目目錄中的Thinkphp架構,預設是不進行遞歸加密,可以根據需求修改
if($file!='.' && $file!='..' && $file!='ThinkPHP')
{
//$path = $target.'\\'.$file;
$sourcePath = $source.'\\'.$file;
$this->decodeDir($sourcePath);
}
}
}else if(is_file($source)){
$extension=substr($source,strrpos($source,'.')+1);
if(strtolower($extension)=='php'){
$this->decode($source);
}else{
//不是php的檔案不處理
//copy($source, $target);
//return;
//遞歸加密 對指定檔案夾下的php檔案加密
public function encodeDir($source,$target){
if(is_dir($source)){
@mkdir($target);
//列出所有檔案并去掉'.'和'..'
$path = $target.'\\'.$file;
$this->encodeDir($sourcePath,$path);
$this->encode($source,$target);
copy($source, $target);
}
$ob = new Encryption();
$ob->source = "D:\php\WWW\workspace\weixin1";
$ob->target = "D:\php\WWW\workspace\weixin2";
//解密指定檔案
//$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);