php代碼的加密解密

來源:互聯網
上載者:User
php 代碼加密類
  1. /*
  2. * @auther:wangyaofeng
  3. * @time:2014.11.6
  4. * @action:對php項目進行加密處理,注意如果項目中存在架構目錄或沒有必要加密的目錄,請提前移出
  5. * */
  6. class Encryption{
  7. private $c='';//儲存密文
  8. private $s='',$q1,$q2,$q3,$q4,$q5,$q6;//儲存產生的加密後的檔案內容
  9. //如果不設定一個值,isset會表示不存在;
  10. private $file='';//讀取檔案的路徑
  11. private $source='',$target='';
  12. //建構函式,執行個體化時調用初始化全域變數;
  13. public function __construct(){
  14. //初始化全域變數
  15. $this->initialVar();
  16. //echo "hello \n";
  17. }
  18. /*
  19. *@input $property_name,$value
  20. *@output
  21. * 魔法方法,對變數進行設定值;可根據需求進行處理。若直接去除if判斷表示可用設定任何屬性的值,包括不存在的屬性;
  22. */
  23. public function __set($property_name,$value){
  24. //定義過的變數;
  25. if(isset($this->$property_name)){
  26. $this->$property_name = $value;
  27. }else{
  28. //異常處理,處理未聲明的變數賦值;可根據需求進行處理。
  29. throw new Exception("property does not exist");
  30. }
  31. }
  32. //魔法方法 取出變數的值;
  33. public function __get($property_name){
  34. if(isset($this->$property_name)){
  35. return $this->$property_name;
  36. }else{
  37. //throw new Exception("property does not exist");
  38. return NULL;
  39. }
  40. }
  41. //取隨機排序
  42. private function RandAbc($length=""){//隨機排序取回
  43. $str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  44. return str_shuffle($str);
  45. }
  46. //對明文內容進行加密處理
  47. private function ciphertext($filename){
  48. //$filename='index.php';
  49. $T_k1=$this->RandAbc();
  50. $T_k2=$this->RandAbc();
  51. $vstr=file_get_contents($filename);
  52. $v1=base64_encode($vstr);
  53. $c=strtr($v1,$T_k1,$T_k2);
  54. $this->c=$T_k1.$T_k2.$c;
  55. return $this;
  56. }
  57. //初始設定變數
  58. private function initialVar(){
  59. $this->q1="O00O0O";//base64_decode
  60. $this->q2="O0O000";//$c(原文經過strtr置換後的密文,由 目標字元+替換字元+base64_encode(‘原文內容’)構成)
  61. $this->q3="O0OO00";//strtr
  62. $this->q4="OO0O00";//substr
  63. $this->q5="OO0000";//52
  64. $this->q6="O00OO0";//urldecode解析過的字串(n1zb/ma5\vt0i28-pxuqy*6%6Crkdg9_ehcswo4+f37j)
  65. }
  66. //產生加密後的模板(複雜版本);
  67. private function model(){
  68. //$c = $this->c;
  69. //$this->initialVar();
  70. $this->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");$'.
  71. $this->q1.'=$'.$this->q6.'{3}.$'.$this->q6.'{6}.$'.$this->q6.'{33}.$'.$this->q6.'{30};$'.$this->q3.'=$'.$this->q6.'{33}.$'.$this->q6.'{10}.$'
  72. .$this->q6.'{24}.$'.$this->q6.'{10}.$'.$this->q6.'{24};$'.$this->q4.'=$'.$this->q3.'{0}.$'.$this->q6.'{18}.$'.$this->q6.'{3}.$'.$this->q3.'{0}
  73. .$'.$this->q3.'小貝.$'.$this->q6.'{24};$'.$this->q5.'=$'.$this->q6.'{7}.$'.$this->q6.'{13};$'.$this->q1.'.=$'.$this->q6.'{22}.$'.$this->q6.'{36}
  74. .$'.$this->q6.'{29}.$'.$this->q6.'{26}.$'.$this->q6.'{30}.$'.$this->q6.'{32}.$'.$this->q6.'{35}.$'.$this->q6.'{26}.$'.$this->q6.'{30};
  75. eval($'.$this->q1.'("'.base64_encode('$'.$this->q2.'="'.$this->c.'";
  76. eval(\'?>\'.$'.$this->q1.'($'.$this->q3.'($'.$this->q4.'($'.$this->q2.',$'.$this->q5.'*2),$'.$this->q4.'($'.$this->q2.',$'.$this->q5.',$'.$this->q5.'),
  77. $'.$this->q4.'($'.$this->q2.',0,$'.$this->q5.'))));').'"));?>';
  78. return $this;
  79. }
  80. //建立加密檔案
  81. private function build($target){
  82. //$this->encodes("./index.php");
  83. //$this->model();
  84. $fpp1 = fopen($target,'w');
  85. fwrite($fpp1,$this->s) or die('寫入是失敗!');
  86. fclose($fpp1);
  87. return $this;
  88. }
  89. //加密處理 連貫操作
  90. public function encode($file,$target){
  91. //$file = "index.php";
  92. //連貫操作其實就是利用函數處理完後返回自身
  93. $this->ciphertext($file)->model()->build($target);
  94. echo 'encode------'.$target.'-----ok
    ';
  95. }
  96. //解密
  97. public function decode($file,$target=''){
  98. //讀取要解密的檔案
  99. $fpp1 = file_get_contents($file);
  100. $this->decodeMode($fpp1)->build($target);
  101. echo 'decode------'.$target.'-----ok
    ';
  102. }
  103. //解密模板,得到解密後的文本
  104. private function decodeMode($fpp1){
  105. //以eval為標誌 截取為數組,前半部分為密文中的替換掉的函數名,後半部分為密文
  106. $m = explode('eval',$fpp1);
  107. //對系統函數的替換部分進行執行,得到系統變數
  108. $varStr = substr($m[0],strpos($m[0],'$'));
  109. //執行後,後續就可以使用替換後的系統函數名
  110. eval($varStr);
  111. //判斷是否有密文
  112. if(!isset($m[1])){
  113. return $this;
  114. }
  115. //對密文進行截取 {$this->q4} substr
  116. $star = strripos($m[1],'(');
  117. $end = strpos($m[1],')');
  118. $str = ${$this->q4}($m[1],$star,$end);
  119. //對密文解密 {$this->q1} base64_decode
  120. $str = ${$this->q1}($str);
  121. //截取出解密後的 核心密文
  122. $evallen = strpos($str,'eval');
  123. $str = substr($str,0,$evallen);
  124. //執行核心密文 使系統變數被賦予值 $O0O000
  125. eval($str);
  126. //並不能將如下段封裝,因為 ${$this->qn} 並不能在全文中起作用
  127. $this->s = ${$this->q1}(
  128. ${$this->q3}(
  129. ${$this->q4}(
  130. ${$this->q2},${$this->q5}*2
  131. ),
  132. ${$this->q4}(
  133. ${$this->q2},${$this->q5},${$this->q5}
  134. ),
  135. ${$this->q4}(
  136. ${$this->q2},0,${$this->q5}
  137. )
  138. )
  139. );
  140. return $this;
  141. }
  142. //遞迴讀取並建立目標目錄結構
  143. private function targetDir($target){
  144. if(!empty($target) ) {
  145. if(!file_exists($target)){
  146. mkdir($target,0777,true);
  147. }else{
  148. chmod($target,0777);
  149. }
  150. }
  151. }
  152. //遞迴解密 對指定檔案夾下的php檔案解密
  153. public function decodeDir($source,$target=""){
  154. if(is_dir($source)){
  155. $this->targetDir($target);
  156. $dir = opendir($source);
  157. while(false!=$file=readdir($dir))
  158. {
  159. //列出所有檔案並去掉'.'和'..' 此處用的執行個體為thinkphp架構,所以預設排除裡Thinkphp目錄,使用者可以按照自己的需求設定
  160. if($file!='.' && $file!='..' && $file !='ThinkPHP')
  161. {
  162. $path = $target.DIRECTORY_SEPARATOR.$file;
  163. $sourcePath = $source.DIRECTORY_SEPARATOR.$file;
  164. $this->decodeDir($sourcePath,$path);
  165. }
  166. }
  167. }else if(is_file($source)){
  168. $extension=substr($source,strrpos($source,'.')+1);
  169. if(strtolower($extension)=='php'){
  170. $this->decode($source,$target);
  171. }else{
  172. //不是php的檔案不處理
  173. copy($source, $target);
  174. }
  175. //return;
  176. }
  177. }
  178. //遞迴加密 對指定檔案夾下的php檔案加密
  179. public function encodeDir($source,$target){
  180. if(is_dir($source)){
  181. $this->targetDir($target);
  182. $dir = opendir($source);
  183. while(false!=$file=readdir($dir))
  184. {
  185. //列出所有檔案並去掉'.'和'..'
  186. if($file!='.' && $file!='..' && $file !='ThinkPHP')
  187. {
  188. $path = $target.DIRECTORY_SEPARATOR.$file;
  189. $sourcePath = $source.DIRECTORY_SEPARATOR.$file;
  190. $this->encodeDir($sourcePath,$path);
  191. }
  192. }
  193. }else if(is_file($source)){
  194. $extension=substr($source,strrpos($source,'.')+1);
  195. if(strtolower($extension)=='php'){
  196. $this->encode($source,$target);
  197. }else{
  198. copy($source, $target);
  199. }
  200. }
  201. }
  202. }
  203. $ob = new Encryption();
  204. $ob->source = "/var/www/bookReservation";
  205. $ob->target = "/var/www/jiami/bookReservation";
  206. //解密指定檔案
  207. //$ob->decode('D:\\php\\WWW\\workspace\\weixin2\\Application\\Home\\Controller\\IndexController.class.php');
  208. //$ob->decode('jiami.php');
  209. //$ob->decode('dam6.php');
  210. //對一個指定的檔案目錄進行加密
  211. $ob->encodeDir($ob->source,$ob->target);
  212. //對一個指定的檔案目錄進行解密
  213. $ob->decodeDir($ob->target,"/var/www/jiami/bookReservationD");
複製代碼

加密解密, php
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.