php在線解密,base64解密后亂碼_php實現php代碼的加密解密

 2023-10-18 阅读 14 评论 0

摘要:php在線解密、php 代碼加密類,大家可以根據自己的需求進行修改,原類如下,是對之前的加密解密類的有一次修改,希望能分享給大家。本次在ubuntu下測試沒有問題,與之前的版本的區別在于,這次的版本更加的通用性。[php] view plain cop

29b3412b98a852c5f0a95cbb56a3c297.png

php在線解密、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");

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/148775.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息