encode_php_files.php
/**
* 加密PHP 文件,加密后的PHP代码无需第三方工具解密,像往常一样,直接运行,不需要另外安装扩展
*
* @author Jimmy Fu hxzx@qq.com
* @copyright 2016-1-12
*/
ini_set('display_errors', '1');
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(0);
header("Content-type:text/html;charset=utf-8"); //调用函数
//$source_php_folder = 'D:\masterphp\renrenrong\app\controller';
$source_php_folder = '/data/wwwroot/renrenrong.com/app/model';
//$encode_php_folder = 'D:\masterphp\code-test\test';
$encode_php_folder = '';
phpfileEncode::$is_rewrite = true; //是否直接加密后替换源文件
$code = new phpfileEncode($source_php_folder,$encode_php_folder);
//$filename = 'D:\masterphp\code-test\sort.php';
//$toFilename = 'D:\masterphp\code-test\test3.php';
//$ret = $code->encodeOneFile($filename,$toFilename=''); //加密单个文件
//var_dump($ret);
//exit;
$list = $code->encode();
exit;function pr($var){
echo '
';
print_r($var);
echo '
';}
function randAbc($length = "") { // 返回随机字符串
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
return str_shuffle($str);
}
/**
* 加密方法类
*
*/
class phpCodeEncode {
/**
* 加密方式1
*/
static public function encode($source){
$contents = $source;
// 去除PHP头部和尾部标识
$headerPos = strpos($contents,'<?php ');
$footerPos = strrpos($contents,'?>');
$contents = substr($contents, $headerPos + 5, $footerPos - $headerPos);
$encode = base64_encode(gzdeflate($contents)); // 开始编码
$encode = '<?php '."\n eval(gzinflate(base64_decode("."'".$encode."'".")));\n\n?>";
return $encode;
}
/**
* 加密方式2
*/
static public function encode2($source){
$T_k1 = randAbc(); //随机密匙1
$T_k2 = randAbc(); //随机密匙2
$v1 = base64_encode($source);
$c = strtr($v1, $T_k1, $T_k2); //根据密匙替换对应字符。
$c = $T_k1.$T_k2.$c;
$q1 = "O00O0O";
$q2 = "O0O000";
$q3 = "O0OO00";
$q4 = "OO0O00";
$q5 = "OO0000";
$q6 = "O00OO0";
$s = '$'.$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");$'.$q1.'=$'.$q6.'{3}.$'.$q6.'{6}.$'.$q6.'{33}.$'.$q6.'{30};$'.$q3.'=$'.$q6.'{33}.$'.$q6.'{10}.$'.$q6.'{24}.$'.$q6.'{10}.$'.$q6.'{24};$'.$q4.'=$'.$q3.'{0}.$'.$q6.'{18}.$'.$q6.'{3}.$'.$q3.'{0}.$'.$q3.'{1}.$'.$q6.'{24};$'.$q5.'=$'.$q6.'{7}.$'.$q6.'{13};$'.$q1.'.=$'.$q6.'{22}.$'.$q6.'{36}.$'.$q6.'{29}.$'.$q6.'{26}.$'.$q6.'{30}.$'.$q6.'{32}.$'.$q6.'{35}.$'.$q6.'{26}.$'.$q6.'{30};eval($'.$q1.'("'.base64_encode('$'.$q2.'="'.$c.'";eval(\'?>\'.$'.$q1.'($'.$q3.'($'.$q4.'($'.$q2.',$'.$q5.'*2),$'.$q4.'($'.$q2.',$'.$q5.',$'.$q5.'),$'.$q4.'($'.$q2.',0,$'.$q5.'))));').'"));';
$s = '<?php '."\n".$s."\n".' ?>';
return $s;
}
}
class phpfileEncode{
static public $source_php_folder; //PHP 项目路径
static public $encode_php_folder; //php 加密后的文件路径
static public $is_rewrite = false; //加密后是否覆盖源文件,默认不覆盖
/**
* 初始化
*/
public function __construct($source_php_folder ,$encode_php_folder = ''){
if(empty($source_php_folder)){
exit('param $source_php_folder not empty!');
}
self::$source_php_folder = $source_php_folder;
if( !is_dir($source_php_folder) ){
exit('"'.$source_php_folder.'" is not a folder!');
}
/**
* 如果单独加密,不覆盖源文件
*/
if(!self::$is_rewrite){
/**
* 如果加密后的文件不制定,则目录下新建目录
*/
if(empty($encode_php_folder)){
$folderName = basename(self::$source_php_folder);
self::$encode_php_folder = dirname(self::$source_php_folder).'/'.$folderName.'_jiami';
}else{
//如果不为空,那么
self::$encode_php_folder = $encode_php_folder;
}
if(!file_exists(self::$encode_php_folder)){
if(!is_writable(dirname(self::$source_php_folder))){
exit('文件夹没有写权限,path:"'.self::$encode_php_folder.'"');
}
mkdir(self::$encode_php_folder,0777,true); //创建文件夹
}
}else{
self::$encode_php_folder = $source_php_folder;
}
}
/**
* 批量加密,
*
*/
public function encode(){
$encode_false_file = $copy_false_file = array();
$encode_ok_num = 0;
$fileList = $this->getFolderFiles();
foreach($fileList as $filepath){
echo $filepath; echo '
';
$newfilepath = str_replace(self::$source_php_folder,self::$encode_php_folder,$filepath);
$newfilepathdir = dirname($newfilepath);
if(!file_exists($newfilepathdir)){
mkdir($newfilepathdir,0777,true); //创建文件夹
}
if(!self::$is_rewrite){
//如果是PHP文件则直接加密,如果是非PHP文件,则执行拷贝
$type=strtolower(substr(strrchr($filepath,'.'),1));
if($type == 'php'){
$ret = $this->encodeOneFile($filepath,$newfilepath);
}else{
//直接拷贝
$ret_cp = copy($filepath,$newfilepath);
if(!$ret_cp){
echo "$filepath,拷贝失败!
\n";
$copy_false_file[$filepath] = array('newpath'=>$newfilepath);
}
}
}else{
//执行加密
$ret = $this->encodeOneFile($filepath,$newfilepath);
}
if(isset($ret) && $ret){
$encode_ok_num++;
echo "$filepath,加密成功!
\n";
}else{
$encode_false_file[$filepath] = array('newpath'=>$newfilepath);
}
}
//有拷贝失败的
if(!empty($copy_false_file)){
echo '有失败的拷贝文件';
pr($copy_false_file);
}
if(!empty($encode_false_file)){
pr($encode_false_file);
}else{
echo '全部加密成功,一共加密完成('.$encode_ok_num.')个文件';
}
}
/**
* 加密一个文件
* @param string $filename 加密源路径
* @param string $toFilename 加密后的文件路径
*
*/
public function encodeOneFile($filename,$toFilename=''){
if(empty($filename)){
return false;
}
if(empty($toFilename)){
$name = basename($filename,'.php');
$new_name = $name.'_jiami.php';
$toFilename = dirname(__FILE__).'/'.$new_name;
}
return $encode_ret = $this->encodeFileContents($filename,$toFilename);
}
/**
* 对PHP源文件加密并输出
*
*/
public function encodeFileContents($filename,$toFilename='') {
if(self::$is_rewrite){
$toFilename = $filename;
}
//如果不可写,则输出
if(!is_writable($toFilename)){
exit('目标文件不可写,paht:'.$toFilename);
}
$type=strtolower(substr(strrchr($filename,'.'),1));
if ('php' == $type && is_file($filename) && is_writable($toFilename)) { // 如果是PHP文件 并且可写 则进行压缩编码
$contents = file_get_contents($filename); // 判断文件是否已经被编码处理
$contents = php_strip_whitespace($filename);
$encode = phpCodeEncode::encode2($contents); //加密函数
return file_put_contents($toFilename, $encode);
}
return false;
}
/**
* 获取文件夹下的文件
*
*/
public function getFolderFiles($file_dir = ''){
static $file_list = array();
if(empty($file_dir)){
$dir = self::$source_php_folder;
}else{
$dir = $file_dir;
}
$list = scandir($dir); // 得到该文件下的所有文件和文件夹
foreach($list as $file){ //遍历,如果不是PHP文件,忽略
$file_location = $dir."/".$file;//生成路径
$type = strtolower(substr(strrchr($file_location,'.'),1));
if(is_dir($file_location) && $file!="." &&$file!=".."){ //判断是不是文件夹
$this->getFolderFiles($file_location); //继续遍历
}
if(is_file($file_location)){
$file_list[] = $file_location;
}
}
return $file_list;
}
} ?>