tonyenc加密源码使用教程

首先从git下载文件下来

https://github.com/lihancong/tonyenc

git clone https://github.com/lihancong/tonyenc

加密前记得备份!!!

因为加密了,原文件全部被加密了。作者已经把解密的方法去掉,所以不可逆的哦,自己要小心文件

将它安装在php扩展里面,

必须大于php7.0以上
这里我系统7.2,如果查看php版本呢?

php -v

[root@VM_0_7_centos lv57]# php -v
PHP 7.2.13 (cli) (built: Dec 17 2018 13:44:46) ( NTS )Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

找到php的php-config文件

find / -name php-config

[root@VM_0_7_centos lv57]# find / -name php-config/www/server/php/72/bin/php-config/www/server/php/72/src/scripts/php-config

使用第一个,把它目录记录下来,等会用到。/www/server/php/72/bin/php-config
下面那个是源。

刚才把tonyenc下载了。

进入这个目录

cd tonyenc

更改加密参数
修改这个core.h文件

/* 这里定制你的加密特征头,不限长度,十六进制哦 */const u_char tonyenc_header[] = {
        0x66, 0x88, 0xff, 0x4f,
        0x68, 0x86, 0x00, 0x56,
        0x11, 0x16, 0x16, 0x18,};/* 这里指定密钥,长一些更安全 */const u_char tonyenc_key[] = {
        0x9f, 0x49, 0x52, 0x00,
        0x58, 0x9f, 0xff, 0x21,
        0x3e, 0xfe, 0xea, 0xfa,
        0xa6, 0x33, 0xf3, 0xc6,};

上面根据他给的,相应改下数字,避免跟其他人一样,安全性就差了,随便加几个或者是改数字即可。

然后,执行php安装扩展命令:phpize

phpize

如果环境找不到phpize,请使用绝对路径/www/server/php/72/bin/phpize

配置到PHP里面

./configure --with-php-config=/www/server/php/72/bin/php-config

--with-php-config 这个目录就是上面刚才查找的那个

最后执行

make && make install

将他配置到php.ini里面
如果不知道php.ini文件,直接搜索

find / -name php.ini

例如我的

[root@VM_0_7_centos bin]# find / -name php.ini/www/server/php/72/etc/php.ini

修改php.ini文件末尾最加刚才的扩展

extension=tonyenc.so

参考

[Zend ZendGuard Loader];php7 do not support zendguardloader @Sep.2015,after support you can uncomment the following line.;zend_extension=/usr/local/zend/php72/ZendGuardLoader.so;zend_loader.enable=1;zend_loader.disable_licensing=0;zend_loader.obfuscation_level_support=3;zend_loader.license_path=;xcache

extension = /www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/fileinfo.so
extension=/www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/exif.so[swoole]extension = swoole.so;上面自带,下面才是新加的 tonyenc.so
extension = tonyenc.so

搞定配置之后,重启动php,nginx。然后执行

[root@VM_0_7_centos bin]# php -m |grep tonyenc

如果有输出

tonyenc

表示安装扩展成功

如何加密

随便在网站目录下新建一个demo文件夹

mkdir /www/wwwroot/demo/

新建文件demo.php
内容

<?phpecho "kongiq你好";?>

新建加密文件,这个文件它提供了,直接复制下来,比如我这个文件名叫:jiami.php

<?php/**
 * tonyenc.php: Encrypt or decrypt the script with tonyenc.
 *
 * A high performance and cross-platform encrypt extension for PHP source code.
 *
 * @author:  Tony
 * @site:    lihancong.cn
 * @github:  github.com/lihancong/tonyenc
 */if (version_compare(PHP_VERSION, 7, '<'))
    die("PHP must later than version 7.0\n");if (php_sapi_name() !== 'cli')
    die("Must run in cli mode\n");if (!extension_loaded('tonyenc'))
    die("The extension: 'tonyenc' not loaded\n");if ($argc <= 1)
    die("\nusage: php tonyenc.php file.php ...     encrypt the php file(s) or directory(s)\n\n");array_shift($argv);foreach ($argv as $fileName) {
    if (is_file($fileName)) {
        handle($fileName);
    } elseif (is_dir($fileName)) {
        $DirectoriesIt = new RecursiveDirectoryIterator($fileName, FilesystemIterator::SKIP_DOTS);
        $AllIt         = new RecursiveIteratorIterator($DirectoriesIt);
        $it            = new RegexIterator($AllIt, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
        foreach ($it as $v)
            handle($v[0]);
    } else {
        echo "Unknowing file: '$fileName'\n";
    }}function handle($file){
    if ($fp = fopen($file, 'rb+') and $fileSize = filesize($file)) {
        $data = tonyenc_encode(fread($fp, $fileSize));
        if ($data !== false) {
            if (file_put_contents($file, '') !== false) {
                rewind($fp);
                fwrite($fp, $data);
            }
        }
        fclose($fp);
    }}

现在加密demo.php那个文件

在命令符输入php jiami.php demo.php
就可以完成demo.php文件加密了

对文件夹下的所有文件加密
比如

config/a.php
config/b.php

命令符直接
php jiami.php config/
就可以完成config的a.php,b.php加密


---------mac mamp 安装tonyenc扩展----


1. php -v  --- php 7.0.15                 ---- 获取当前php版本号,注意必须是大于7.0的
2. sudo find / -name phpize           ---- /Applications/MAMP/bin/php/php7.0.15/bin/phpize
3. sudo find / -name php.ini           ---- /Applications/MAMP/conf/php7.0.15/php.ini
4. sudo find / -name php-config     ---- /Applications/MAMP/bin/php/php7.0.15/bin/php-config
5. cd encrytion   ---  进入扩展包目录下
6. 执行 /Applications/MAMP/bin/php/php7.0.15/bin/phpize
7. 执行 ./configure --with-php-config=/Applications/MAMP/bin/php/php7.0.15/bin/php-config
8. make && make install       ------- 记录扩展安装目录:     /Applications/MAMP/bin/php/php7.0.15/lib/php/extensions/no-debug-non-zts-20151012/
9. 添加扩展文件 tonyenc.so 到 php.ini 文件最后  extension= /Applications/MAMP/bin/php/php7.0.15/lib/php/extensions/no-debug-non-zts-20151012/tonyenc.so
10.重启php 重启 服务器 php -m 查看是否有  tonyenc 有就说明安装成功了