Chinaunix首页 | 论坛 | 博客
  • 博客访问: 203379
  • 博文数量: 63
  • 博客积分: 1002
  • 博客等级: 准尉
  • 技术积分: 640
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 20:34
文章分类

全部博文(63)

文章存档

2017年(2)

2015年(3)

2013年(4)

2012年(21)

2011年(33)

分类: PHP

2015-05-08 17:08:55

分享2种PHP的源码加密方式,此加密方法支持任意PHP版。

注意,加密后的PHP代码无需第三方工具解密,像往常一样,直接运行即可。

  复制代码 代码如下:


  1. <?php
  2.  function encode_file_contents($filename) {
  3.      $type=strtolower(substr(strrchr($filename,'.'),1));
  4.      if ('php' == $type && is_file($filename) && is_writable($filename)) { // 如果是PHP文件 并且可写 则进行压缩编码
  5.          $contents = file_get_contents($filename); // 判断文件是否已经被编码处理
  6.          $contents = php_strip_whitespace($filename);

  7.          // 去除PHP头部和尾部标识
  8.          $headerPos = strpos($contents,');
  9.          $footerPos = strrpos($contents,'?>');
  10.          $contents = substr($contents, $headerPos + 5, $footerPos - $headerPos);
  11.          $encode = base64_encode(gzdeflate($contents)); // 开始编码
  12.          $encode = '."\n eval(gzinflate(base64_decode("."'".$encode."'".")));\n\n?>";

  13.          return file_put_contents($filename, $encode);
  14.      }
  15.      return false;
  16.  }

  17.  //调用函数
  18.  $filename = 'dam.php';
  19.  encode_file_contents($filename);
  20.  echo "OK,加密完成!"
  21.  ?>



加密方式2:

  复制代码 代码如下:


  1. <?php

  2.  function RandAbc($length = "") { // 返回随机字符串
  3.      $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  4.      return str_shuffle($str);
  5.  }

  6.  $filename = 'index.php'; //要加密的文件
  7.  $T_k1 = RandAbc(); //随机密匙1
  8.  $T_k2 = RandAbc(); //随机密匙2
  9.  $vstr = file_get_contents($filename);
  10.  $v1 = base64_encode($vstr);
  11.  $c = strtr($v1, $T_k1, $T_k2); //根据密匙替换对应字符。
  12.  $c = $T_k1.$T_k2.$c;
  13.  $q1 = "O00O0O";
  14.  $q2 = "O0O000";
  15.  $q3 = "O0OO00";
  16.  $q4 = "OO0O00";
  17.  $q5 = "OO0000";
  18.  $q6 = "O00OO0";
  19.  $s = '$'.$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");$'.$q1.'=$'.$q6.'{3}.$'.$q6.'{6}.$'.$q6.'{33}.$'.$q6.'{30};$'.$q3.'=$'.$q6.'{33}.$'.$q6.'{10}.$'.$q6.'{24}.$'.$q6.'{10}.$'.$q6.'{24};$'.$q4.'=$'.$q3.'{0}.$'.$q6.'{18}.$'.$q6.'{3}.$'.$q3.'{0}.$'.$q3.'{1}.$'.$q6.'{24};$'.$q5.'=$'.$q6.'{7}.$'.$q6.'{13};$'.$q1.'.=$'.$q6.'{22}.$'.$q6.'{36}.$'.$q6.'{29}.$'.$q6.'{26}.$'.$q6.'{30}.$'.$q6.'{32}.$'.$q6.'{35}.$'.$q6.'{26}.$'.$q6.'{30};eval($'.$q1.'("'.base64_encode('$'.$q2.'="'.$c.'";eval(\'?>\'.$'.$q1.'($'.$q3.'($'.$q4.'($'.$q2.',$'.$q5.'*2),$'.$q4.'($'.$q2.',$'.$q5.',$'.$q5.'),$'.$q4.'($'.$q2.',0,$'.$q5.'))));').'"));';

  20.  $s = '."\n".$s."\n".' ?>';
  21.  //echo $s;
  22.  // 生成 加密后的PHP文件
  23.  $fpp1 = fopen('temp_'.$filename, 'w');
  24.  fwrite($fpp1, $s) or die('写文件错误');

  25.  ?>



其实,PHP加密源码方式有很多,譬如,免费的微盾PHP加密,还有 搞的在线加密,只是phpjm更复杂点而已。

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

另外,分享一个 PHP类,它能对 文本的内容进行 二进制加密 与 解密,代码如下:

  复制代码 代码如下:


  1. <?php

  2.  class text_auth
  3.  {
  4.      var $n_iter;

  5.      function text_auth()
  6.      {
  7.          $this->setIter(32);
  8.      }

  9.      function setIter($n_iter)
  10.      {
  11.          $this->n_iter = $n_iter;
  12.      }

  13.      function getIter()
  14.      {
  15.          return $this->n_iter;
  16.      }

  17.      function encrypt($data, $key)
  18.      {
  19.          $n = $this->_resize($data, 4);

  20.          $data_long[0] = $n;
  21.          $n_data_long = $this->_str2long(1, $data, $data_long);

  22.          $n = count($data_long);
  23.          if (($n & 1) == 1) {
  24.              $data_long[$n] = chr(0);
  25.              $n_data_long++;
  26.          }

  27.          $this->_resize($key, 16, true);
  28.          if ( '' == $key )
  29.              $key = '0000000000000000';

  30.          $n_key_long = $this->_str2long(0, $key, $key_long);

  31.          $enc_data = '';
  32.          $w = array(0, 0);
  33.          $j = 0;
  34.          $k = array(0, 0, 0, 0);
  35.          for ($i = 0; $i < $n_data_long; ++$i) {
  36.              if ($j + 4 <= $n_key_long) {
  37.                  $k[0] = $key_long[$j];
  38.                  $k[1] = $key_long[$j + 1];
  39.                  $k[2] = $key_long[$j + 2];
  40.                  $k[3] = $key_long[$j + 3];
  41.              } else {
  42.                  $k[0] = $key_long[$j % $n_key_long];
  43.                  $k[1] = $key_long[($j + 1) % $n_key_long];
  44.                  $k[2] = $key_long[($j + 2) % $n_key_long];
  45.                  $k[3] = $key_long[($j + 3) % $n_key_long];
  46.              }
  47.              $j = ($j + 4) % $n_key_long;

  48.              $this->_encipherLong($data_long[$i], $data_long[++$i], $w, $k);

  49.              $enc_data .= $this->_long2str($w[0]);
  50.              $enc_data .= $this->_long2str($w[1]);
  51.          }

  52.          return $enc_data;
  53.      }

  54.      function decrypt($enc_data, $key)
  55.      {
  56.          $n_enc_data_long = $this->_str2long(0, $enc_data, $enc_data_long);

  57.          $this->_resize($key, 16, true);
  58.          if ( '' == $key )
  59.              $key = '0000000000000000';

  60.          $n_key_long = $this->_str2long(0, $key, $key_long);

  61.          $data = '';
  62.          $w = array(0, 0);
  63.          $j = 0;
  64.          $len = 0;
  65.          $k = array(0, 0, 0, 0);
  66.          $pos = 0;

  67.          for ($i = 0; $i < $n_enc_data_long; $i += 2) {
  68.              if ($j + 4 <= $n_key_long) {
  69.                  $k[0] = $key_long[$j];
  70.                  $k[1] = $key_long[$j + 1];
  71.                  $k[2] = $key_long[$j + 2];
  72.                  $k[3] = $key_long[$j + 3];
  73.              } else {
  74.                  $k[0] = $key_long[$j % $n_key_long];
  75.                  $k[1] = $key_long[($j + 1) % $n_key_long];
  76.                  $k[2] = $key_long[($j + 2) % $n_key_long];
  77.                  $k[3] = $key_long[($j + 3) % $n_key_long];
  78.              }
  79.              $j = ($j + 4) % $n_key_long;

  80.              $this->_decipherLong($enc_data_long[$i], $enc_data_long[$i + 1], $w, $k);

  81.              if (0 == $i) {
  82.                  $len = $w[0];
  83.                  if (4 <= $len) {
  84.                      $data .= $this->_long2str($w[1]);
  85.                  } else {
  86.                      $data .= substr($this->_long2str($w[1]), 0, $len % 4);
  87.                  }
  88.              } else {
  89.                  $pos = ($i - 1) * 4;
  90.                  if ($pos + 4 <= $len) {
  91.                      $data .= $this->_long2str($w[0]);

  92.                      if ($pos + 8 <= $len) {
  93.                          $data .= $this->_long2str($w[1]);
  94.                      } elseif ($pos + 4 < $len) {
  95.                          $data .= substr($this->_long2str($w[1]), 0, $len % 4);
  96.                      }
  97.                  } else {
  98.                      $data .= substr($this->_long2str($w[0]), 0, $len % 4);
  99.                  }
  100.              }
  101.          }
  102.          return $data;
  103.      }

  104.      function _encipherLong($y, $z, &$w, &$k)
  105.      {
  106.          $sum = (integer) 0;
  107.          $delta = 0x9E3779B9;
  108.          $n = (integer) $this->n_iter;

  109.          while ($n-- > 0) {
  110.              $y = $this->_add($y,
  111.                                    $this->_add($z << 4 ^ $this->_rshift($z, 5), $z) ^
  112.                                      $this->_add($sum, $k[$sum & 3]));
  113.              $sum = $this->_add($sum, $delta);
  114.              $z = $this->_add($z,
  115.                                    $this->_add($y << 4 ^ $this->_rshift($y, 5), $y) ^
  116.                                      $this->_add($sum, $k[$this->_rshift($sum, 11) & 3]));
  117.          }

  118.          $w[0] = $y;
  119.          $w[1] = $z;
  120.      }

  121.      function _decipherLong($y, $z, &$w, &$k)
  122.      {
  123.          $sum = 0xC6EF3720;
  124.          $delta = 0x9E3779B9;
  125.          $n = (integer) $this->n_iter;

  126.          while ($n-- > 0) {
  127.              $z = $this->_add($z,
  128.                                    -($this->_add($y << 4 ^ $this->_rshift($y, 5), $y) ^
  129.                                          $this->_add($sum, $k[$this->_rshift($sum, 11) & 3])));
  130.              $sum = $this->_add($sum, -$delta);
  131.              $y = $this->_add($y,
  132.                                    -($this->_add($z << 4 ^ $this->_rshift($z, 5), $z) ^
  133.                                          $this->_add($sum, $k[$sum & 3])));
  134.          }

  135.          $w[0] = $y;
  136.          $w[1] = $z;
  137.      }

  138.      function _resize(&$data, $size, $nonull = false)
  139.      {
  140.          $n = strlen($data);
  141.          $nmod = $n % $size;
  142.          if ( 0 == $nmod )
  143.              $nmod = $size;

  144.          if ($nmod > 0) {
  145.              if ($nonull) {
  146.                  for ($i = $n; $i < $n - $nmod + $size; ++$i) {
  147.                      $data[$i] = $data[$i % $n];
  148.                  }
  149.              } else {
  150.                  for ($i = $n; $i < $n - $nmod + $size; ++$i) {
  151.                      $data[$i] = chr(0);
  152.                  }
  153.              }
  154.          }
  155.          return $n;
  156.      }

  157.      function _hex2bin($str)
  158.      {
  159.          $len = strlen($str);
  160.          return pack('H' . $len, $str);
  161.      }

  162.      function _str2long($start, &$data, &$data_long)
  163.      {
  164.          $n = strlen($data);

  165.          $tmp = unpack('N*', $data);
  166.          $j = $start;

  167.          foreach ($tmp as $value)
  168.              $data_long[$j++] = $value;

  169.          return $j;
  170.      }

  171.      function _long2str($l)
  172.      {
  173.          return pack('N', $l);
  174.      }

  175.      function _rshift($integer, $n)
  176.      {
  177.          if (0xffffffff < $integer || -0xffffffff > $integer) {
  178.              $integer = fmod($integer, 0xffffffff + 1);
  179.          }

  180.          if (0x7fffffff < $integer) {
  181.              $integer -= 0xffffffff + 1.0;
  182.          } elseif (-0x80000000 > $integer) {
  183.              $integer += 0xffffffff + 1.0;
  184.          }

  185.          if (0 > $integer) {
  186.              $integer &= 0x7fffffff;
  187.              $integer >>= $n;
  188.              $integer |= 1 << (31 - $n);
  189.          } else {
  190.              $integer >>= $n;
  191.          }

  192.          return $integer;
  193.      }

  194.      function _add($i1, $i2)
  195.      {
  196.          $result = 0.0;

  197.          foreach (func_get_args() as $value) {
  198.              if (0.0 > $value) {
  199.                  $value -= 1.0 + 0xffffffff;
  200.              }

  201.              $result += $value;
  202.          }

  203.          if (0xffffffff < $result || -0xffffffff > $result) {
  204.              $result = fmod($result, 0xffffffff + 1);
  205.          }

  206.          if (0x7fffffff < $result) {
  207.              $result -= 0xffffffff + 1.0;
  208.          } elseif (-0x80000000 > $result) {
  209.              $result += 0xffffffff + 1.0;
  210.          }
  211.          return $result;
  212.      }
  213.  }
  214.  ?>


使用方法参考如下:

  1. // 加密过程
  2. view sourceprint?
  3.  $text_file = S_ROOT . './456.php';
  4.  $str = @file_get_contents($text_file);

  5.  require_once S_ROOT . "./text_auth.php";
  6.  $text_auth = new text_auth(64);

  7.  $str = $text_auth->encrypt($str, "");

  8.  $filename = S_ROOT . './789.php'; // 加密后的文本为二进制,普通的文本编辑器无法正常查看
  9.  file_put_contents($filename, $str);
  10. // 解密过程
  11. view sourceprint
  12. ?01 $text_file = S_ROOT . './789.php';
  13.  $str = @file_get_contents($text_file);

  14.  require_once S_ROOT . "./text_auth.php";
  15.  $text_auth = new text_auth(64);

  16.  $str = $text_auth->decrypt($str, "");

  17.  $filename = S_ROOT . './456.php';
  18.  file_put_contents($filename, $str);


阅读(1952) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~