合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# BLENC扩展 (代码加密 实验型) [BLENC下载](https://pecl.php.net/package/BLENC) phpinfo查看是非线程安全版本: ![](https://img.kancloud.cn/77/55/77557e40761a6bf8385537c8ecdffbe6_509x21.png) 这个已经没有维护了?目前只支持5.6 将解压后拿到的php\_blenc.dll放入php/ext文件夹下 php.ini配置; 名字默认可修改范围更新日志[blenc.key\_file](https://www.php.net/manual/zh/blenc.configuration.php#ini.blenc.key-file)/usr/local/etc/blenckeysPHP\_INI\_ALL包含可用解密密钥列表的文件。这个文件必须是可读的``` <pre class="calibre10">``` <span class="token3">[</span>blenc<span class="token3">]</span> extension<span class="token1">=</span>php_blenc<span class="token3">.</span>dll blenc<span class="token3">.</span>key_file <span class="token1">=</span> <span class="token2">"D:\phpstudy_pro\WWW\keys"</span> ``` ``` # 预定义常量 **`BLENC_EXT_VERSION(string)`** # Blenc 函数 **blenc\_encrypt**( string $plaintext , string $encodedfile \[, string $encryption\_key \] ) : string—用BLENC加密PHP脚本 # 例子: demo.php ``` <pre class="calibre10">``` <span class="token">/* 获取要加密的文件 */</span> $source_code <span class="token1">=</span> <span class="token4">file_get_contents</span><span class="token3">(</span><span class="token2">"./test1.php"</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//一定要去掉<?php 和 ?></span> $source_code <span class="token1">=</span> <span class="token4">preg_replace</span><span class="token3">(</span><span class="token2">'/^<'</span><span class="token3">.</span><span class="token2">'\?php\s+/'</span><span class="token3">,</span> <span class="token2">''</span><span class="token3">,</span> $source_code<span class="token3">)</span><span class="token3">;</span> $source_code <span class="token1">=</span> <span class="token4">preg_replace</span><span class="token3">(</span><span class="token2">'/\s+\?>\s*$/'</span><span class="token3">,</span> <span class="token2">''</span><span class="token3">,</span> $source_code<span class="token3">)</span><span class="token3">;</span> <span class="token4">var_dump</span><span class="token3">(</span>$source_code<span class="token3">)</span><span class="token3">;</span> <span class="token">/* 创建加密文件 第三个参数存在时生成的固定的密钥 */</span> $redistributable_key <span class="token1">=</span> <span class="token4">blenc_encrypt</span><span class="token3">(</span>$source_code<span class="token3">,</span> <span class="token2">"test_encoded.php"</span><span class="token3">,</span>'qaEm35CDymPcHNbx7cq1Xn24ZfdDlBConj1QtsZR5T4<span class="token1">=</span> '<span class="token3">)</span><span class="token3">;</span> <span class="token">/* 读取ini设置的key_file文件 */</span> $key_file <span class="token1">=</span> <span class="token4">ini_get</span><span class="token3">(</span><span class="token2">'blenc.key_file'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">/* 保存可重新分发的密钥到key_file文件 */</span> <span class="token4">file_put_contents</span><span class="token3">(</span>$key_file<span class="token3">,</span> $redistributable_key<span class="token3">.</span><span class="token2">"\n"</span><span class="token3">,</span> FILE_APPEND<span class="token3">)</span><span class="token3">;</span> ``` ``` 密钥被存入keys文件中 ``` <pre class="calibre10">``` <span class="token">//当blenc_encrypt第三个参数存在时则会生成固定的密钥如:</span> S6NdXvj2jlbyQE3MIwwWqzPDh<span class="token1">/</span>vMWgY0S49LTB3U<span class="token1">+</span><span class="token6">6</span>wx7Yh<span class="token1">+</span>Z48znZYTAmmNxEVx 当第三个参数不存在时,则随机生成如下密钥: jSJvgPQ75ekGQKXizpX9nKWSanTkOvbUkaD1vHMj2Xo<span class="token1">=</span> ``` ``` 如果在本机测试加密文件,则需要重启apache,否则也会报如下错误! ``` <pre class="calibre17">``` Warning<span class="token3">:</span> blenc_compile<span class="token3">:</span> Validation of script <span class="token2">'D:/phpstudy_pro/WWW/www.test.com/audit/test_encoded.php'</span> failed<span class="token3">.</span> MD5_FILE<span class="token3">:</span> <span class="token6">6</span>fb72f51bcb8c82857a291b8d7fb34b6 MD5_CALC<span class="token3">:</span> a26481ba5bc3ef7449d3a2d46d2ca19c <span class="token5">in</span> Unknown on line <span class="token6">0</span> Fatal error<span class="token3">:</span> blenc_compile<span class="token3">:</span> Validation of script <span class="token2">'D:/phpstudy_pro/WWW/www.test.com/audit/test_encoded.php'</span> failed<span class="token3">,</span> cannot execute<span class="token3">.</span> <span class="token5">in</span> Unknown on line <span class="token6">0</span> ``` ```