基于php实现的php代码加密解密类完整实例


Posted in PHP onOctober 12, 2016

本文实例讲述了基于php实现的php代码加密解密类。分享给大家供大家参考,具体如下:

php 代码加密类,大家可以根据自己的需求进行修改,原类如下,该实例在ubuntu下测试没有问题。

<?php
class Encryption{
    private $c='';//存储密文
    private $s='',$q1,$q2,$q3,$q4,$q5,$q6;//存储生成的加密后的文件内容
    //如果不设置一个值,isset会表示不存在;
    private $file='';//读取文件的路径
    private $source='',$target='';
    //构造函数,实例化时调用初始化全局变量;
    public function __construct(){
      //初始化全局变量
      $this->initialVar();
      //echo "hello \n";
    }
    /*
    *@input $property_name,$value
    *@output
    *  魔法方法,对变量进行设置值;可根据需求进行处理。若直接去除if判断表示可用设置任何属性的值,包括不存在的属性;
    */
    public function __set($property_name,$value){
      //定义过的变量;
      if(isset($this->$property_name)){
        $this->$property_name = $value;
      }else{
        //异常处理,处理未声明的变量赋值;可根据需求进行处理。
        throw new Exception("property does not exist");
      }
    }
    //魔法方法 取出变量的值;
    public function __get($property_name){
      if(isset($this->$property_name)){
        return $this->$property_name;
      }else{
        //throw new Exception("property does not exist");
        return NULL;
      }
    }
    //取随机排序
    private function RandAbc($length=""){//随机排序取回
     $str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
     return str_shuffle($str);
    }
    //对明文内容进行加密处理
    private function ciphertext($filename){
      //$filename='index.php';
      $T_k1=$this->RandAbc();
      $T_k2=$this->RandAbc();
      $vstr=file_get_contents($filename);
      $v1=base64_encode($vstr);
      $c=strtr($v1,$T_k1,$T_k2);
      $this->c=$T_k1.$T_k2.$c;
      return $this;
    }
    //初始化变量
    private function initialVar(){
      $this->q1="O00O0O";//base64_decode
      $this->q2="O0O000";//$c(原文经过strtr置换后的密文,由 目标字符+替换字符+base64_encode(‘原文内容')构成)
      $this->q3="O0OO00";//strtr
      $this->q4="OO0O00";//substr
      $this->q5="OO0000";//52
      $this->q6="O00OO0";//urldecode解析过的字符串(n1zb/ma5\vt0i28-pxuqy*6%6Crkdg9_ehcswo4+f37j)
    }
    //生成加密后的模板(复杂版本);
    private function model(){
      //$c = $this->c;
      //$this->initialVar();
      $this->s='<?php $'.$this->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");$'.
      $this->q1.'=$'.$this->q6.'{3}.$'.$this->q6.'{6}.$'.$this->q6.'{33}.$'.$this->q6.'{30};$'.$this->q3.'=$'.$this->q6.'{33}.$'.$this->q6.'{10}.$'
      .$this->q6.'{24}.$'.$this->q6.'{10}.$'.$this->q6.'{24};$'.$this->q4.'=$'.$this->q3.'{0}.$'.$this->q6.'{18}.$'.$this->q6.'{3}.$'.$this->q3.'{0}
      .$'.$this->q3.'{1}.$'.$this->q6.'{24};$'.$this->q5.'=$'.$this->q6.'{7}.$'.$this->q6.'{13};$'.$this->q1.'.=$'.$this->q6.'{22}.$'.$this->q6.'{36}
      .$'.$this->q6.'{29}.$'.$this->q6.'{26}.$'.$this->q6.'{30}.$'.$this->q6.'{32}.$'.$this->q6.'{35}.$'.$this->q6.'{26}.$'.$this->q6.'{30};
      eval($'.$this->q1.'("'.base64_encode('$'.$this->q2.'="'.$this->c.'";
      eval(\'?>\'.$'.$this->q1.'($'.$this->q3.'($'.$this->q4.'($'.$this->q2.',$'.$this->q5.'*2),$'.$this->q4.'($'.$this->q2.',$'.$this->q5.',$'.$this->q5.'),
      $'.$this->q4.'($'.$this->q2.',0,$'.$this->q5.'))));').'"));?>';
      return $this;
    }
    //创建加密文件
    private function build($target){
      //$this->encodes("./index.php");
      //$this->model();
      $fpp1 = fopen($target,'w');
      fwrite($fpp1,$this->s) or die('写入是失败!');
      fclose($fpp1);
      return $this;
    }
    //加密处理 连贯操作
    public function encode($file,$target){
      //$file = "index.php";
      //连贯操作其实就是利用函数处理完后返回自身
      $this->ciphertext($file)->model()->build($target);
      echo 'encode------'.$target.'-----ok<br/>';
    }
    //解密
    public function decode($file,$target=''){
      //读取要解密的文件
      $fpp1 = file_get_contents($file);
      $this->decodeMode($fpp1)->build($target);
      echo 'decode------'.$target.'-----ok<br/>';
    }
    //解密模板,得到解密后的文本
    private function decodeMode($fpp1){
      //以eval为标志 截取为数组,前半部分为密文中的替换掉的函数名,后半部分为密文
      $m = explode('eval',$fpp1);
      //对系统函数的替换部分进行执行,得到系统变量
      $varStr = substr($m[0],strpos($m[0],'$'));
      //执行后,后续就可以使用替换后的系统函数名
      eval($varStr);
      //判断是否有密文
      if(!isset($m[1])){
        return $this;
      }
      //对密文进行截取 {$this->q4} substr
      $star = strripos($m[1],'(');
      $end = strpos($m[1],')');
      $str = ${$this->q4}($m[1],$star,$end);
      //对密文解密 {$this->q1} base64_decode
      $str = ${$this->q1}($str);
      //截取出解密后的 核心密文
      $evallen = strpos($str,'eval');
      $str = substr($str,0,$evallen);
      //执行核心密文 使系统变量被赋予值 $O0O000
      eval($str);
      //并不能将如下段封装,因为 ${$this->qn} 并不能在全文中起作用
      $this->s = ${$this->q1}(
        ${$this->q3}(
          ${$this->q4}(
            ${$this->q2},${$this->q5}*2
          ),
          ${$this->q4}(
            ${$this->q2},${$this->q5},${$this->q5}
          ),
          ${$this->q4}(
            ${$this->q2},0,${$this->q5}
          )
        )
      );
      return $this;
    }
    //递归读取并创建目标目录结构
    private function targetDir($target){
      if(!empty($target) ) {
        if(!file_exists($target)){
          mkdir($target,0777,true);
        }else{
          chmod($target,0777);
        }
      }
    }
    //递归解密 对指定文件夹下的php文件解密
    public function decodeDir($source,$target=""){
      if(is_dir($source)){
        $this->targetDir($target);
        $dir = opendir($source);
        while(false!=$file=readdir($dir))
        {
          //列出所有文件并去掉'.'和'..' 此处用的实例为thinkphp框架,所以默认排除里Thinkphp目录,用户可以按照自己的需求设置
          if($file!='.' && $file!='..' && $file !='ThinkPHP')
          {
            $path = $target.DIRECTORY_SEPARATOR.$file;
            $sourcePath = $source.DIRECTORY_SEPARATOR.$file;
            $this->decodeDir($sourcePath,$path);
          }
        }
      }else if(is_file($source)){
        $extension=substr($source,strrpos($source,'.')+1);
        if(strtolower($extension)=='php'){
          $this->decode($source,$target);
        }else{
          //不是php的文件不处理
          copy($source, $target);
        }
        //return;
      }
    }
    //递归加密 对指定文件夹下的php文件加密
    public function encodeDir($source,$target){
      if(is_dir($source)){
        $this->targetDir($target);
        $dir = opendir($source);
        while(false!=$file=readdir($dir))
        {
          //列出所有文件并去掉'.'和'..'
          if($file!='.' && $file!='..' && $file !='ThinkPHP')
          {
            $path = $target.DIRECTORY_SEPARATOR.$file;
            $sourcePath = $source.DIRECTORY_SEPARATOR.$file;
            $this->encodeDir($sourcePath,$path);
          }
        }
      }else if(is_file($source)){
        $extension=substr($source,strrpos($source,'.')+1);
        if(strtolower($extension)=='php'){
          $this->encode($source,$target);
        }else{
          copy($source, $target);
        }
      }
    }
}
$ob = new Encryption();
$ob->source = "/var/www/bookReservation";
$ob->target = "/var/www/jiami/bookReservation";
//解密指定文件
//$ob->decode('D:\\php\\WWW\\workspace\\weixin2\\Application\\Home\\Controller\\IndexController.class.php');
//$ob->decode('jiami.php');
//$ob->decode('dam6.php');
//对一个指定的文件目录进行加密
$ob->encodeDir($ob->source,$ob->target);
//对一个指定的文件目录进行解密
$ob->decodeDir($ob->target,"/var/www/jiami/bookReservationD");

PHP 相关文章推荐
php strtotime 函数UNIX时间戳
Jan 14 PHP
php 动态添加记录
Mar 10 PHP
php下用cookie统计用户访问网页次数的代码
May 09 PHP
解析PHP中empty is_null和isset的测试
Jun 29 PHP
destoon后台网站设置变成空白的解决方法
Jun 21 PHP
PHP反向代理类代码
Aug 15 PHP
php获得文件大小和文件创建时间的方法
Mar 13 PHP
深入理解PHP中的Streams工具
Jul 03 PHP
php类自动加载器实现方法
Jul 28 PHP
PHP获取路径和目录的方法总结【必看篇】
Mar 04 PHP
php处理静态页面:页面设置缓存时间实例
Jun 22 PHP
PHP实现的文件浏览器功能简单示例
Sep 12 PHP
php fseek函数读取大文件两种方法
Oct 12 #PHP
PHP从二维数组得到N层分类树的实现代码
Oct 11 #PHP
php 无限分类 树形数据格式化代码
Oct 11 #PHP
PHP简单判断iPhone、iPad、Android及PC设备的方法
Oct 11 #PHP
PHP中SERIALIZE和JSON的序列化与反序列化操作区别分析
Oct 11 #PHP
php外部执行命令函数用法小结
Oct 11 #PHP
php字符串操作常见问题小结
Oct 11 #PHP
You might like
PHP实现15位身份证号转18位的方法分析
2019/10/16 PHP
CentOS7系统搭建LAMP及更新PHP版本操作详解
2020/03/26 PHP
php使用自带dom扩展进行元素匹配的原理解析
2020/05/29 PHP
jquery移动listbox的值原理及代码
2013/05/03 Javascript
jQuery函数的等价原生函数代码示例
2013/05/27 Javascript
jquery遍历数组与筛选数组的方法
2013/11/05 Javascript
如何设置一定时间内只能发送一次请求
2014/02/28 Javascript
jQuery插件Elastislide实现响应式的焦点图无缝滚动切换特效
2015/04/12 Javascript
在JavaScript中处理字符串之link()方法的使用
2015/06/08 Javascript
详解JavaScript中的forEach()方法的使用
2015/06/08 Javascript
jQuery中each()、find()和filter()等节点操作方法详解(推荐)
2016/05/25 Javascript
jquery插件格式实例分析
2016/06/16 Javascript
JSON与js对象序列化实例详解
2017/03/16 Javascript
详解jquery选择器的原理
2017/08/01 jQuery
Vue.js单向绑定和双向绑定实例分析
2018/08/14 Javascript
微信小程序支付PHP代码
2018/08/23 Javascript
JS实现吸顶特效
2020/01/08 Javascript
Python多维/嵌套字典数据无限遍历的实现
2016/11/04 Python
Python Socket传输文件示例
2017/01/16 Python
Django中利用filter与simple_tag为前端自定义函数的实现方法
2017/06/15 Python
Python正则表达式分组概念与用法详解
2017/06/24 Python
Linux下python与C++使用dlib实现人脸检测
2018/06/29 Python
使用Python处理BAM的方法
2018/09/28 Python
python使用xlrd模块读取xlsx文件中的ip方法
2019/01/11 Python
python读写Excel表格的实例代码(简单实用)
2019/12/19 Python
Python网络爬虫四大选择器用法原理总结
2020/06/01 Python
CSS3 animation ? steps 函数详解
2019/08/30 HTML / CSS
英国标志性奢侈品牌:Burberry
2016/07/28 全球购物
写一个函数,要求输入一个字符串和一个字符长度,对该字符串进行分隔
2015/07/30 面试题
分公司经理岗位职责
2013/11/11 职场文书
自主招生自荐信范文
2013/12/04 职场文书
销售工作岗位职责
2013/12/24 职场文书
2014年体育教师工作总结
2014/12/03 职场文书
伏羲庙导游词
2015/02/09 职场文书
小学语文教师竞聘演讲稿范文
2019/08/09 职场文书
Netty分布式客户端接入流程初始化源码分析
2022/03/25 Java/Android