PHP

您所在的位置:网站首页 php加密系统源码怎么用 PHP

PHP

2023-03-17 18:21| 来源: 网络整理| 查看: 265

2449059-433f7bd316a9d80c.png php-beast.png 前言

首先说说为什么要用PHP-Beast?有时候我们的代码会放到代理商上, 所以很有可能代码被盗取,或者我们写了一个商业系统而且不希望代码开源,所以这时候就需要加密我们的代码。另外PHP-Beast是完全免费和开源的, 当其不能完成满足你的需求时, 可以修改其代码而满足你的要。

编译安装如下

注意:如果你需要使用,首先修改key。可以参考下文

Linux编译安装: $ wget https://github.com/liexusong/php-beast/archive/master.zip $ unzip master.zip $ cd php-beast-master $ phpize $ ./configure $ sudo make && make install

编译好之后修改php.ini配置文件, 加入配置项: extension=beast.so, 重启php-fpm 。

配置项: beast.cache_size = size beast.log_file = "path_to_log" beast.log_user = "user" beast.enable = On beast.log_level支持参数: 1. DEBUG 2. NOTICE 3. ERROR 支持的模块有: 1. AES 2. DES 3. Base64 通过测试环境: Nginx + Fastcgi + (PHP-5.2.x ~ PHP-7.1.x) 怎么加密你的项目 加密方案1:

安装完 php-beast 后可以使用 tools 目录下的 encode_files.php 来加密你的项目。使用 encode_files.php 之前先修改 tools 目录下的 configure.ini 文件,如下:

; source path src_path = "" ; destination path dst_path = "" ; expire time expire = "" ; encrypt type (selection: DES, AES, BASE64) encrypt_type = "DES"

src_path 是要加密项目的路径,dst_path 是保存加密后项目的路径,expire 是设置项目可使用的时间 (expire 的格式是:YYYY-mm-dd HH:ii:ss)。encrypt_type是加密的方式,选择项有:DES、AES、BASE64。 修改完 configure.ini 文件后就可以使用命令 php encode_files.php 开始加密项目。

加密方案2:

使用beast_encode_file()函数加密文件,函数原型如下:

beast_encode_file(string $input_file, string $output_file, int expire_timestamp, int encrypt_type)。 $input_file: 要加密的文件 $output_file: 输出的加密文件路径 $expire_timestamp: 文件过期时间戳 $encrypt_type: 加密使用的算法(支持:BEAST_ENCRYPT_TYPE_DES、BEAST_ENCRYPT_TYPE_AES) 制定自己的php-beast

php-beast 有多个地方可以定制的,以下一一列出:

使用 header.c 文件可以修改 php-beast 加密后的文件头结构,这样网上的解密软件就不能认识我们的加密文件,就不能进行解密,增加加密的安全性。

php-beast 提供只能在指定的机器上运行的功能。要使用此功能可以在 networkcards.c 文件添加能够运行机器的网卡号,例如:

char *allow_networkcards[] = { "fa:16:3e:08:88:01", NULL, };

这样设置之后,php-beast 扩展就只能在 fa:16:3e:08:88:01 这台机器上运行。另外要注意的是,由于有些机器网卡名可能不一样,所以如果你的网卡名不是 eth0 的话,可以在 php.ini 中添加配置项: beast.networkcard = "xxx" 其中 xxx 就是你的网卡名,也可以配置多张网卡,如:beast.networkcard = "eth0,eth1,eth2"。

使用 php-beast 时最好不要使用默认的加密key,因为扩展是开源的,如果使用默认加密key的话,很容易被人发现。所以最好编译的时候修改加密的key,aes模块 可以在 aes_algo_handler.c 文件修改,而 des模块 可以在 des_algo_handler.c 文件修改。 函数列表 & Debug 开启debug模式:

可以在configure时加入 --enable-beast-debug 选项来开启debug模式。开启debug模式后需要在php.ini配置文件中加入配置项:beast.debug_path 和 beast.debug_mode。beast.debug_mode 用于指定是否使用debug模式,而 beast.debug_path 用于输出解密后的php脚本源码。这样就可以在 beast.debug_path 目录中看到php-beast解密后的源代码,可以方便知道扩展解密是否正确。

函数列表: beast_encode_file(): 用于加密一个文件 beast_avail_cache(): 获取可以缓存大小 beast_support_filesize(): 获取beast支持的最大可加密文件大小 beast_file_expire(): 获取一个文件的过期时间 beast_clean_cache(): 清空beast的所有缓存(如果有文件更新, 可以使用此函数清空缓存) 修改默认加密的key

1,修改加密后的文件头结构:打开header.c文件,找到以下代码:

char encrypt_file_header_sign[] = { 0xe8, 0x16, 0xa4, 0x0c, 0xf2, 0xb2, 0x60, 0xee };

int encrypt_file_header_length = sizeof(encrypt_file_header_sign);自定义修改以下代码(其中的数字的范围为:0-8,字母的范围为:a-f):

0xe8, 0x16, 0xa4, 0x0c, 0xf2, 0xb2, 0x60, 0xee

2,修改aes模块加密key:打开php-beast-master/aes_algo_handler.c文件,找到以下代码:

static uint8_t key[] = { 0x2b, 0x7e, 0x61, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xi7, 0x10, 0x88, 0x09, 0xcf, 0xef, 0xxc, };

自定义修改以下代码(其中的数字的范围为:0-8,字母的范围为:a-f):

0x2b, 0x7e, 0x61, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xi7, 0x10, 0x88, 0x09, 0xcf, 0xef, 0xxc,

3,修改des模块加密key:打开php-beast-master/des_algo_handler.c文件,找到以下代码:

static char key[8] = { 0x21, 0x1f, 0xe1, 0x1f, 0xy1, 0x9e, 0x01, 0x0e, };

自定义修改以下代码(其中的数字的范围为:0-8,字母的范围为:a-f):

0x21, 0x1f, 0xe1, 0x1f, 0xy1, 0x9e, 0x01, 0x0e,

4,修改base64模块加密key:打开php-beast-master/base64_algo_handler.c文件,自定义修改以下代码:

static const short base64_reverse_table[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; php-beast自定义加密模块 一,首先创建一个.c的文件。例如我们要编写一个使用base64加密的模块,可以创建一个名叫base64_algo_handler.c的文件。然后在文件添加如下代码: #include "beast_module.h" int base64_encrypt_handler(char *inbuf, int len, char **outbuf, int *outlen) { ... } int base64_decrypt_handler(char *inbuf, int len, char **outbuf, int *outlen) { ... } void base64_free_handler(void *ptr) { ... } struct beast_ops base64_handler_ops = { .name = "base64-algo", .encrypt = base64_encrypt_handler, .decrypt = base64_decrypt_handler, .free = base64_free_handler, };

模块必须实现3个方法,分别是:encrypt、decrypt、free方法。encrypt方法负责把inbuf字符串加密,然后通过outbuf输出给beast。decrypt方法负责把加密数据inbuf解密,然后通过outbuf输出给beast。free方法负责释放encrypt和decrypt方法生成的数据

二,写好我们的加密模块后,需要在global_algo_modules.c添加我们模块的信息。代码如下: #include #include "beast_module.h" extern struct beast_ops des_handler_ops; extern struct beast_ops base64_handler_ops; struct beast_ops *ops_handler_list[] = { &des_handler_ops, &base64_handler_ops, /* 这里是我们的模块信息 */ NULL, }; 三,修改config.m4文件,修改倒数第二行,如下代码:

PHP_NEW_EXTENSION(beast, beast.c des_algo_handler.c beast_mm.c spinlock.c cache.c beast_log.c global_algo_modules.c * base64_algo_handler.c *, $ext_shared)

base64_algo_handler.c的代码是我们添加的,这里加入的是我们模块的文件名。现在大功告成了,可以编译试下。如果要使用我们刚编写的加密算法来加密php文件,可以修改php.ini文件的配置项,如下:``beast.encrypt_handler = "base64-algo"`

名字就是我们模块的name。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3