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

本文实例讲述了基于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");

PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:

密码安全性在线检测:
http://tools.jb51.net/password/my_password_safe

高强度密码生成器:
http://tools.jb51.net/password/CreateStrongPassword

MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password

迅雷、快车、旋风URL加密/解密工具:
http://tools.jb51.net/password/urlrethunder

在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php加密方法总结》、《PHP编码与转码操作技巧汇总》、《php面向对象程序设计入门教程》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

(0)

相关推荐

  • 微盾PHP脚本加密专家php解密算法

    复制代码 代码如下: <?php /*********************************** *威盾PHP加密专家解密算法 By:Neeao *http://Neeao.com *2009-09-10 ***********************************/ $filename="play-js.php";//要解密的文件 $lines = file($filename);//0,1,2行 //第一次base64解密 $content="&

  • 怎样给PHP源代码加密?PHP二进制加密与解密的解决办法

    分享2种PHP的源码加密方式,此加密方法支持任意PHP版. 注意,加密后的PHP代码无需第三方工具解密,像往常一样,直接运行即可. 复制代码 代码如下: <?php   function encode_file_contents($filename) {       $type=strtolower(substr(strrchr($filename,'.'),1));       if ('php' == $type && is_file($filename) &&

  • php基于mcrypt的加密解密实例

    本文实例讲述了php基于mcrypt实现加密解密的方法.分享给大家供大家参考.具体实现方法如下: PHP中自带了相当多的加密的方法,这里我们来看一下mcrypt扩展的使用方式.也是在工作中需要用这个东西加密访问用户的Cookie的值,认真的学习了这个方面的内容. 1.简介 Mcrypt是PHP的一个扩展,完成了常用加密算法的封装.其实该扩展是对mcrypt标准类库的封装,mcrypt完成了相当多的常用加密算法,如DES, TripleDES, Blowfish (default), 3-WAY,

  • php中AES加密解密的例子小结

    aesDemo.php: 例子, 复制代码 代码如下: <?phprequire_once('./AES.php');//$aes = new AES();$aes = new AES(true);// 把加密后的字符串按十六进制进行存储//$aes = new AES(true,true);// 带有调试信息且加密字符串按十六进制存储$key = "this is a 32 byte key";// 密钥$keys = $aes->makeKey($key);$encod

  • 2个比较经典的PHP加密解密函数分享

    项目中有时我们需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理. 最常见的应用在用户登录以及一些API数据交换的场景. 笔者收录了一些比较经典的PHP加密解密函数代码,分享给大家.加密解密原理一般都是通过一定的加密解密算法,将密钥加入到算法中,最终得到加密解密结果. 1.非常给力的authcode加密函数,Discuz!经典代码(带详解): 复制代码 代码如下: function authcode

  • 一组PHP可逆加密解密算法实例代码

    对于大部分密码加密,我们可以采用md5.sha1等方法.可以有效防止数据泄露,但是这些方法仅适用于无需还原的数据加密. 对于需要还原的信息,则需要采用可逆的加密解密算法. 下面一组PHP函数是实现此加密解密的方法: 加密算法如下: 复制代码 代码如下: function encrypt($data, $key){ $key = md5($key);    $x  = 0;    $len = strlen($data);    $l  = strlen($key);    for ($i = 0

  • php中base64_decode与base64_encode加密解密函数实例

    本文实例讲述了php中base64_decode与base64_encode加密解密函数.分享给大家供大家参考.具体分析如下: 这两个函数在php中是用得对php代码进行加密与解密码的base64_encode是加密,而base64_decode是解密了,下面我们看两个简单实例. base64_encode语法:string base64_decode(string data); 复制代码 代码如下: $str='d3d3LmpiNTEubmV0IOiEmuacrOS5i+Wutg==';   

  • PHP 加密与解密的斗争

    但是PHP反编译系统的出现却迅速引起了Zend公司甚至整个PHP用户群的恐慌,包括上述产品在内的几乎所有大型PHP产品全部出现了破解版本甚至出现了完整的源代码.如此一来,Zend不得不向Cracker们低头,承认所有的加密技术都有破解的办法,并承诺对加密产品进行改进以便提供更强的保护. PHP加密技术可以追溯到01年,当时PHP已经很火爆,所以许多公司希望能推出商业化的PHP系统,于是PHP加密(编译)产品应运而生,随后PHP大颚Zend公司推出了自己的Zend Encoder,直到现在,Zen

  • PHP 加密解密内部算法

    将它们打包成一个文件就叫fun.php吧 复制代码 代码如下: <?php function passport_encrypt($txt, $key) { srand((double)microtime() * 1000000); $encrypt_key = md5(rand(0, 32000)); $ctr = 0; $tmp = ''; for($i = 0;$i < strlen($txt); $i++) { $ctr = $ctr == strlen($encrypt_key) ?

  • PHP中加密解密函数与DES加密解密实例

    本文实例讲述了PHP中加密解密函数与DES加密解密的应用,分享给大家供大家参考.具体如下: 例子,php加密解密的例子 加密函数: 复制代码 代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数  srand((double)microtime() * 1000000);  $encrypt_key=md5(rand(0, 32000));  $ctr=0;  $t

随机推荐