这两天突发奇想想要用php写一个对日常项目加密以及解密的功能,经过努力简单的封装了一个对php代码进行加密解密的类,一些思想也是来自于网络,初步测试用着还行,可以实现对指定项目的加密以及解密(只针对本类中加密的解密)过程,在这里分享给大家,水平比较有限那里有错误还希望指出,共同提高,一下会给大家列出来实现的思想以及源码。

加密过程:
读取源文件,base64_encode进行加密,利用混排得到的52个大小写字母作为秘钥进行替换
$c=strtr(密文,对应待替换的字母,要替换成的字母);
将两个秘钥和密文链接起来形成要加密文件的主要内容
最后根据提前写好的模板格式,分别对base64_decode,strtr,substr几个命令进行组合,并将组和好的密文放入模板并在此base64_encode加密,
写入要加密的文件。

解密过程:
读取要解密的文件,截取出以eval开头的字符串,之后层次截取得到加密模板中生成的密文
base64_decode解密得到解密后的明文
截取得到的名文,将源文件形成的 秘钥+密文那段的字符截取出来,通过eval执行截取的字符使密文赋值给预定义的变量($O0O000)
通过执行base64_decode(strtr(substr($O0O000,52*2),substr($O0O000,52,52),substr($O0O000,0,52)));
进行解密,将解密过的内容(明文)写入文件。

总结:其实整个加密解密的过程都是互逆的,加密后的php运行是通过eval()这个函数实现的,解密的过程就是加密的逆过程。


  1. <?php
  2. /*
  3. * @auther:wangyaofeng
  4. * @time:2014.11.6
  5. * @action:对php项目进行加密处理
  6. * @Example:
  7. *
  8. * $ob = new Encryption();
  9. * $ob->source = "D:\php\WWW\workspace\weixin1";
  10. * $ob->target = "D:\php\WWW\workspace\weixin2";
  11. * $ob->decode('D:\\php\\WWW\\workspace\\weixin2\\Application\\Home\\Controller\\IndexController.class.php');
  12. * $ob->decode('jiami.php');
  13. * $ob->decode('dam6.php');
  14. * $ob->encodeDir($ob->source,$ob->target);
  15. * $ob->decodeDir($ob->target);
  16. *
  17. * */
  18. class Encryption{
  19. private $c='';//存储密文
  20. private $s='',$q1,$q2,$q3,$q4,$q5,$q6;//存储生成的加密后的文件内容
  21. //如果不设置一个值,isset会表示不存在;
  22. private $file='';//读取文件的路径
  23. private $source='',$target='';
  24. //构造函数,实例化时调用初始化全局变量;
  25. public function __construct(){
  26. //初始化全局变量
  27. $this->initialVar();
  28. //echo "hello \n";
  29. }
  30. /*
  31. *@input  $property_name,$value
  32. *@output
  33. *   魔法方法,对变量进行设置值;可根据需求进行处理。直接取出if判断表示可用设置任何属性的值,包括不存在的属性;
  34. */
  35. public function __set($property_name,$value){
  36. //定义过的变量;
  37. if(isset($this->$property_name)){
  38. $this->$property_name = $value;
  39. }else{
  40. //异常处理,处理未声明的变量赋值;可根据需求进行处理。
  41. throw new Exception("property does not exist");
  42. }
  43. }
  44. //魔法方法 取出变量的值;
  45. public function __get($property_name){
  46. if(isset($this->$property_name)){
  47. return $this->$property_name;
  48. }else{
  49. //throw new Exception("property does not exist");
  50. return NULL;
  51. }
  52. }
  53. //取随机排序
  54. private function RandAbc($length=""){//随机排序取回
  55. $str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  56. return str_shuffle($str);
  57. }
  58. //加密处理
  59. private function ciphertext($filename){
  60. //$filename='index.php';
  61. $T_k1=$this->RandAbc();
  62. $T_k2=$this->RandAbc();
  63. $vstr=file_get_contents($filename);
  64. $v1=base64_encode($vstr);
  65. $c=strtr($v1,$T_k1,$T_k2);
  66. //$this->$c 为错误写法 会报错Cannot access empty property
  67. //正确写法 $this->c;
  68. $this->c=$T_k1.$T_k2.$c;
  69. return $this;
  70. }
  71. //生成加密后的模板(简单版本);
  72. private function modelEasy(){
  73. $this->s='<?php
  74. '.'$c='
  75. .'"'.$this->c.'";'."\n".
  76. '$m='
  77. .
  78. 'strtr(substr($c,52*2),substr($c,52,52),substr($c,0,52));'."\n"
  79. .
  80. '$s=base64_decode('
  81. .
  82. '$m'
  83. .
  84. ');'.
  85. 'eval("'.'?>".$s'.');'.
  86. '
  87. ?>';
  88. return $this;
  89. }
  90. //初始化变量
  91. private function initialVar(){
  92. $this->q1="O00O0O";//base64_decode
  93. $this->q2="O0O000";//$c(原文经过strtr置换后的密文,由 目标字符+替换字符+base64_encode(‘原文内容’)构成)
  94. $this->q3="O0OO00";//strtr
  95. $this->q4="OO0O00";//substr
  96. $this->q5="OO0000";//52
  97. $this->q6="O00OO0";//urldecode解析过的字符串(n1zb/ma5\vt0i28-pxuqy*6%6Crkdg9_ehcswo4+f37j)
  98. }
  99. //生成加密后的模板(复杂版本);
  100. private function model(){
  101. $c = $this->c;
  102. //$this->initialVar();
  103. $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");$'.
  104. $this->q1.'=$'.$this->q6.'{3}.$'.$this->q6.'{6}.$'.$this->q6.'{33}.$'.$this->q6.'{30};$'.$this->q3.'=$'.$this->q6.'{33}.$'.$this->q6.'{10}.$'
  105. .$this->q6.'{24}.$'.$this->q6.'{10}.$'.$this->q6.'{24};$'.$this->q4.'=$'.$this->q3.'{0}.$'.$this->q6.'{18}.$'.$this->q6.'{3}.$'.$this->q3.'{0}
  106. .$'.$this->q3.'{1}.$'.$this->q6.'{24};$'.$this->q5.'=$'.$this->q6.'{7}.$'.$this->q6.'{13};$'.$this->q1.'.=$'.$this->q6.'{22}.$'.$this->q6.'{36}
  107. .$'.$this->q6.'{29}.$'.$this->q6.'{26}.$'.$this->q6.'{30}.$'.$this->q6.'{32}.$'.$this->q6.'{35}.$'.$this->q6.'{26}.$'.$this->q6.'{30};
  108. eval($'.$this->q1.'("'.base64_encode('$'.$this->q2.'="'.$c.'";
  109. eval(\'?>\'.$'.$this->q1.'($'.$this->q3.'($'.$this->q4.'($'.$this->q2.',$'.$this->q5.'*2),$'.$this->q4.'($'.$this->q2.',$'.$this->q5.',$'.$this->q5.'),
  110. $'.$this->q4.'($'.$this->q2.',0,$'.$this->q5.'))));').'"));?>';
  111. return $this;
  112. }
  113. //创建加密文件
  114. private function build($file,$target){
  115. //$this->encodes("./index.php");
  116. //$this->model();
  117. $fpp1 = fopen($target,'w');
  118. fwrite($fpp1,$this->s) or die('写入是失败!');
  119. return $this;
  120. }
  121. //加密处理 连贯操作
  122. public function encode($file,$target){
  123. //$file = "index.php";
  124. //连贯操作其实就是利用函数处理完后返回自身
  125. $this->ciphertext($file)->model()->build($file,$target);
  126. echo 'encode------'.$target.'-----ok<br/>';
  127. }
  128. //解密
  129. public function decode($file,$target=''){
  130. //导入文件(类名不应该和导入文件的类名相同)
  131. //$file = ($target=='')?$file:$target;
  132. /*require_once("$file");
  133. //写入文件中的解析后的值(简单版的解密直接是$s)
  134. //fwrite($fpp1,$s) or die('写入失败!');
  135. //初始化系统常量
  136. //$this->initialVar();
  137. //
  138. if(${$this->q2}){
  139. $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})));
  140. }else{
  141. $s = file_get_contents($file);
  142. }
  143. //打开文件
  144. $fpp1 = fopen($file,'w');
  145. //写入文件
  146. fwrite($fpp1,$s) or die('写入失败!');*/
  147. //读取要解密的文件
  148. $fpp1 = file_get_contents($file);
  149. //截取以 eval开头的部分
  150. preg_match('[eval.*]', $fpp1,$m);
  151. if(!isset($m[0])){
  152. return;
  153. }
  154. $len = strlen($m[0]);
  155. //将eval开头的字符串中间的加密字符截取出来
  156. $str = substr($m[0],strlen('eval($O00O0O('),$len-18);
  157. //对密文进行解密
  158. $str = base64_decode($str);
  159. //截取出解密后的  核心密文
  160. $str = substr($str,0 ,strlen($str)-120);
  161. //执行核心密文
  162. eval($str);
  163. //解密
  164. $s = base64_decode(strtr(substr($O0O000,52*2),substr($O0O000,52,52),substr($O0O000,0,52)));
  165. //写入文件
  166. $fpp1 = fopen($file,'w');
  167. fwrite($fpp1,$s) or die('写入失败!');
  168. echo 'decode------'.$target.'-----ok<br/>';
  169. }
  170. //递归解密 对指定文件夹下的php文件解密
  171. public function decodeDir($source,$target=""){
  172. //$target = $source;
  173. if(is_dir($source)){
  174. //mkdir($target);
  175. $dir = opendir($source);
  176. while(false!=$file=readdir($dir))
  177. {
  178. //列出所有文件并去掉'.'和'..' Thinkphp 表示项目目录中的Thinkphp框架,默认是不进行递归加密,可以根据需求修改
  179. if($file!='.' && $file!='..' && $file!='ThinkPHP')
  180. {
  181. //$path = $target.'\\'.$file;
  182. $sourcePath =  $source.'\\'.$file;
  183. $this->decodeDir($sourcePath);
  184. }
  185. }
  186. }else if(is_file($source)){
  187. $extension=substr($source,strrpos($source,'.')+1);
  188. if(strtolower($extension)=='php'){
  189. $this->decode($source);
  190. }else{
  191. //不是php的文件不处理
  192. //copy($source, $target);
  193. }
  194. //return;
  195. }
  196. }
  197. //递归加密 对指定文件夹下的php文件加密
  198. public function encodeDir($source,$target){
  199. if(is_dir($source)){
  200. @mkdir($target);
  201. $dir = opendir($source);
  202. while(false!=$file=readdir($dir))
  203. {
  204. //列出所有文件并去掉'.'和'..'
  205. if($file!='.' && $file!='..' && $file!='ThinkPHP')
  206. {
  207. $path = $target.'\\'.$file;
  208. $sourcePath =  $source.'\\'.$file;
  209. $this->encodeDir($sourcePath,$path);
  210. }
  211. }
  212. }else if(is_file($source)){
  213. $extension=substr($source,strrpos($source,'.')+1);
  214. if(strtolower($extension)=='php'){
  215. $this->encode($source,$target);
  216. }else{
  217. copy($source, $target);
  218. }
  219. //return;
  220. }
  221. }
  222. }
  223. $ob = new Encryption();
  224. $ob->source = "D:\php\WWW\workspace\weixin1";
  225. $ob->target = "D:\php\WWW\workspace\weixin2";
  226. //解密指定文件
  227. //$ob->decode('D:\\php\\WWW\\workspace\\weixin2\\Application\\Home\\Controller\\IndexController.class.php');
  228. //$ob->decode('jiami.php');
  229. //$ob->decode('dam6.php');
  230. //对一个指定的文件目录进行加密
  231. //$ob->encodeDir($ob->source,$ob->target);
  232. //对一个指定的文件目录进行解密
  233. //$ob->decodeDir($ob->target);

php加密解密功能类的更多相关文章

  1. C# MD5加密解密帮助类

    /// <summary>    /// MD5加密解密帮助类    /// </summary>    public static class DESHelper    {  ...

  2. php加密解密处理类

    [PHP]代码 <?php /*=========================================================== = 版权协议: = GPL (The GN ...

  3. AES加密解密 助手类 CBC加密模式

    "; string result1 = AESHelper.AesEncrypt(str); string result2 = AESHelper.AesDecrypt(result1); ...

  4. .Net(c#)加密解密工具类:

    /// <summary> /// .Net加密解密帮助类 /// </summary> public class NetCryptoHelper { #region des实 ...

  5. Base64加密解密工具类

    使用Apache commons codec类Base64进行加密解密 maven依赖 <dependency> <groupId>commons-codec</grou ...

  6. php实现的三个常用加密解密功能函数示例

    目录 算法一: 算法二: 算法三(改进第一个加密之后的算法) 本文实例讲述了php实现的三个常用加密解密功能函数.分享给大家供大家参考,具体如下: 算法一: //加密函数 function lock_ ...

  7. C#做的一个加密/解密的类

    转自:http://www.16aspx.com/Article/3904 using System; using System.Security.Cryptography; using System ...

  8. [DEncrypt] C# DEncrypt加密/解密帮助类(转载)

    点击下载 DEncrypt.rar 这个类是关于加密,解密的操作,文件的一些高级操作1.使用 缺省密钥字符串 加密/解密string2.使用 给定密钥字符串 加密/解密string3.使用 缺省密钥字 ...

  9. 加密解密工具类(Java,DES)

    一个Java版的DES加密工具类,能够用来进行网络传输数据加密,保存password的时候进行加密. import java.security.Key; import java.security.sp ...

随机推荐

  1. [Shell]Bash变量:变量测试与内容替换

    --------------------------------------------------------------------------------------------- 变量测试是通 ...

  2. FMDB基本应用

    1.打开数据库 #import "ViewController.h" #import "FMDB.h" @interface ViewController () ...

  3. Comet:基于 HTTP 长连接的“服务器推”技术

    “服务器推”技术的应用 请访问 Ajax 技术资源中心,这是有关 Ajax 编程模型信息的一站式中心,包括很多文档.教程.论坛.blog.wiki 和新闻.任何 Ajax 的新信息都能在这里找到. c ...

  4. 2004FBI树

    题目描述 Description 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树[1],它的结点类型 ...

  5. 奇葩问题之ToolBar返回键失效

    今天遇到一个奇葩问题,先说现象:接口调用成功,但是在后台业务上报错时(比如:手机号已经被注册过时,接口调用成功,但是后台返回了错误信息:手机号已被注册),toolBar的返回键失效了. 后来仔细看了一 ...

  6. Sudoku Solver Backtracking

    该博客好好分析 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicat ...

  7. Spring MVC 指导文档解读(一)

    22.1 指导文档章节 In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, whic ...

  8. andriod终端操作命令

    1.卸载大厅安装包,如果卸载不掉的话,添加-r pm uninstall -r com.scigames.egm.lobby 2.查找包含"scigames"的安装包 3.卸载一个 ...

  9. halcon摄像机标定

    摄像机标定程序: 注意:E:/calibration_image :为标定图像文件路径       'E:/calibration_description/caltab_123mm.descr:为标定 ...

  10. Orchard Express Oracle v1.7.2 发布

    发布说明: 1. 添加Oracle支持,在AppData目录下提供Oracle及Sql Server数据库创建脚本. 2. 修正上一版本(精简版 v1.7.2)中,Dashboard无需登录问题. O ...