PHP 不跳转界面取input值进行验证_php实现php代码的加密解密

0fc59b3afd9a9b21767a8a0b2c210c4b.png

php 代码加密类,大家可以根据自己的需求进行修改,原类如下,是对之前的加密解密类的有一次修改,希望能分享给大家。本次在ubuntu下测试没有问题,与之前的版本的区别在于,这次的版本更加的通用性。

[php] view plain copy

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值