使用php blenc扩展加密php源代码,不但可以加密源代码,还可以设置过期时间。
扩展地址 http://pecl.php.net/package/blenc
注意:不要启用opcache扩展,否则加密后的代码无法正常运行。
要加密的源码不需要<?php 及?>开始与结束标记
可指定个人密钥。
可指定一个BLENC模块过期日期。 你可以决定BLENC 模块将工作到一个日期。之后,BLENC不会解密任何文件.
这些都可以在头文件 blenc_protect.h中设置,设置好后再编译BLENC.
用法:
http://cn2.php.net/manual/en/function.blenc-encrypt.php
安装用phpize安装 phpize ./configure make make install.
注意,如果安装后,访问php出现500错误,请试着打开 php.ini中的expose_php=On;
安装扩展后,要使以下文件可写:
/usr/local/etc/blenckeys
具体加密用法文件:
<?php
$file='en.php';
$file_name = basename($file);
$unencrypted_key='itvyyb';
$source_code = file_get_contents($file);
$html='';
const TARGET_DIR='/Library/WebServer/Documents/';
//This covers old-asp tags, php short-tags, php echo tags, and normal php tags.
$contents = preg_replace(array('/^<(\?|\%)\=?(php)?/', '/(\%|\?)>$/'), array('',''), $source_code);
//echo $contents;exit;
$html .= "<br> BLENC blowfish unencrypted key: $unencrypted_key" . PHP_EOL;
$html .= "<br> BLENC file to encode: " . $file_name . PHP_EOL;
//file_put_contents('blencode-log', "---\nFILE: $file_name\nSIZE: ".strlen($contents)."\nMD5: ".md5($contents)."\n", FILE_APPEND);
$redistributable_key = blenc_encrypt($contents, TARGET_DIR . '/blenc/' . $file_name, $unencrypted_key);
$html .= "<br> BLENC size of content: " . strlen($contents) . PHP_EOL;
/**
* Server key
* key_file.blenc
*/
file_put_contents(TARGET_DIR . '/blenc/' . 'key_file.blenc', $redistributable_key . PHP_EOL);
$html .= "<br> BLENC redistributable key file key_file.blenc updated." . PHP_EOL;
exec("cat key_file.blenc >> /usr/local/etc/blenckeys");
echo $html;
?>
另一个php加密扩展https://github.com/liexusong/php-beast
和这个差不多。
php-beast 加密代码使用方法如下,在cli下运行
<?php
function getext($file) {
$list = explode('.', $file);
return strtolower($list[count($list)-1]);
}
function encrypt_project($project, $new_project) {
$project = rtrim($project, '/');
$new_project = rtrim($new_project, '/');
if (!file_exists($new_project)) {
if (!mkdir($new_project)) {
printf("[failed] failed to call `mkdir()' function\n");
return false;
}
}
$hdl_o = opendir($project);
$hdl_n = opendir($new_project);
if (!$hdl_o || !$hdl_n) {
if ($hdl_o) closedir($hdl_o);
if ($hdl_n) closedir($hdl_n);
printf("[failed] failed to call `opendir()' function\n");
return false;
}
while (($file = readdir($hdl_o)) !== false) {
if ($file == '.' || $file == '..') {
continue;
}
$path = $project.'/'.$file;
if (is_dir($path)) {
encrypt_project($path, $new_project.'/'.$file);
} elseif (is_file($path) && getext($file) == 'php') {
beast_encode_file($path, $new_project.'/'.$file);
} else {
copy($path, $new_project.'/'.$file);
}
}
closedir($hdl_o);
closedir($hdl_n);
return true;
}
$stdin = fopen("php://stdin", "r");
$stdout = fopen("php://stdout", "w");
if (!$stdin || !$stdout) {
if ($stdin) fclose($stdin);
if ($stdout) fclose($stdout);
exit("[failed] failed to open I/O stream\n");
}
fwrite($stdout, "Please enter project path: ");
$project = fgets($stdin);
$project = trim($project);
fwrite($stdout, "Please enter output project path: ");
$new_project = fgets($stdin);
$new_project = trim($new_project);
$start = time();
fwrite($stdout, "Encrypting...\n");
encrypt_project($project, $new_project); /* encrypt project */
$spend = time() - $start;
fwrite($stdout, "Finish encrypt, spend {$spend} seconds.\n");
?>
good:
<?php
/**
* FileName: encrypt_project.php
* Desc: php源码加密
* Author: Lenix
* Email: yangyb@itv.cn
* HomePage: http://www.itv.com.cn
* Version: 1.0
* LastChange: 2015-08-03 13:34:56
* History:
*/
function getext($file) {
$list = explode('.', $file);
return strtolower($list[count($list)-1]);
}
function encrypt_project($project, $new_project) {
$unencrypted_key='itvyyb';//个人密钥
$project = rtrim($project, '/');
$new_project = rtrim($new_project, '/');
if (!file_exists($new_project)) {
if (!mkdir($new_project)) {
printf("错误,不能调用 `mkdir()' 函数\n");
return false;
}
}
$hdl_o = opendir($project);
$hdl_n = opendir($new_project);
if (!$hdl_o || !$hdl_n) {
if ($hdl_o) closedir($hdl_o);
if ($hdl_n) closedir($hdl_n);
printf("错误,不能调用`opendir()' 函数\n");
return false;
}
while (($file = readdir($hdl_o)) !== false) {
if ($file == '.' || $file == '..') {
continue;
}
$path = $project.'/'.$file;
if (is_dir($path)) {
encrypt_project($path, $new_project.'/'.$file);
} elseif (is_file($path) && getext($file) == 'php') {
$source_code = file_get_contents($path);
$contents = preg_replace(array('/^<(\?|\%)\=?(php)?/', '/(\%|\?)>$/'), array('',''), $source_code);
$redistributable_key = blenc_encrypt($contents, $new_project.'/'.$file, $unencrypted_key);
file_put_contents( 'key_file.blenc', $redistributable_key . PHP_EOL);
$dir=dirname(__FILE__);
exec("cat $dir/key_file.blenc >> /usr/local/etc/blenckeys");
} else {
copy($path, $new_project.'/'.$file);
}
}
closedir($hdl_o);
closedir($hdl_n);
return true;
}
$stdin = fopen("php://stdin", "r");
$stdout = fopen("php://stdout", "w");
if (!$stdin || !$stdout) {
if ($stdin) fclose($stdin);
if ($stdout) fclose($stdout);
exit("错误,不能打开 I/O 流\n");
}
fwrite($stdout, "请输入项目路径");
$project = fgets($stdin);
$project = trim($project);
fwrite($stdout, "请输入加密后项目路径: ");
$new_project = fgets($stdin);
$new_project = trim($new_project);
$start = time();
fwrite($stdout, "加密ing...\n");
encrypt_project($project, $new_project);
$spend = time() - $start;
fwrite($stdout, "完成加密, 用时 {$spend} 秒.\n");
?>
最后更新于 2022年6月26日