Home > Backend Development > PHP Tutorial > How to implement source code encryption in php

How to implement source code encryption in php

小云云
Release: 2023-03-17 07:52:01
Original
1928 people have browsed it

Method one:

<?php
  function RandAbc($length=""){//返回随机字符串
  $str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  return str_shuffle($str);
 }
 $filepath=&#39;index.php&#39;;
 $path_parts= pathinfo($filepath);
 $filename=$path_parts["basename"];
 $T_k1=RandAbc();//随机密匙1
 $T_k2=RandAbc();//随机密匙2
 $vstr=file_get_contents($filename);//要加密的文件 
 $v1=base64_encode($vstr);
 $c=strtr($v1,$T_k1,$T_k2);//根据密匙替换对应字符。
 $c=$T_k1.$T_k2.$c;
 $q1="O00O0O";
 $q2="O0O000";
 $q3="O0OO00";
 $q4="OO0O00";
 $q5="OO0000";
 $q6="O00OO0";
 $s=&#39;$&#39;.$q6.&#39;=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");$&#39;.$q1.&#39;=$&#39;.$q6.&#39;{3}.$&#39;.$q6.&#39;{6}.$&#39;.$q6.&#39;{33}.$&#39;.$q6.&#39;{30};$&#39;.$q3.&#39;=$&#39;.$q6.&#39;{33}.$&#39;.$q6.&#39;{10}.$&#39;.$q6.&#39;{24}.$&#39;.$q6.&#39;{10}.$&#39;.$q6.&#39;{24};$&#39;.$q4.&#39;=$&#39;.$q3.&#39;{0}.$&#39;.$q6.&#39;{18}.$&#39;.$q6.&#39;{3}.$&#39;.$q3.&#39;{0}.$&#39;.$q3.&#39;{1}.$&#39;.$q6.&#39;{24};$&#39;.$q5.&#39;=$&#39;.$q6.&#39;{7}.$&#39;.$q6.&#39;{13};$&#39;.$q1.&#39;.=$&#39;.$q6.&#39;{22}.$&#39;.$q6.&#39;{36}.$&#39;.$q6.&#39;{29}.$&#39;.$q6.&#39;{26}.$&#39;.$q6.&#39;{30}.$&#39;.$q6.&#39;{32}.$&#39;.$q6.&#39;{35}.$&#39;.$q6.&#39;{26}.$&#39;.$q6.&#39;{30};eval($&#39;.$q1.&#39;("&#39;.base64_encode(&#39;$&#39;.$q2.&#39;="&#39;.$c.&#39;";eval(\&#39;?>\&#39;.$&#39;.$q1.&#39;($&#39;.$q3.&#39;($&#39;.$q4.&#39;($&#39;.$q2.&#39;,$&#39;.$q5.&#39;*2),$&#39;.$q4.&#39;($&#39;.$q2.&#39;,$&#39;.$q5.&#39;,$&#39;.$q5.&#39;),$&#39;.$q4.&#39;($&#39;.$q2.&#39;,0,$&#39;.$q5.&#39;))));&#39;).&#39;"));&#39;;
 $s=&#39;<?
 &#39;.$s.
&#39;
 ?>&#39;;
 echo $s;
 //生成 加密后的PHP文件
 !is_dir(&#39;create/&#39;) && mkdir(&#39;create/&#39;);
 $fpp1 = fopen(&#39;create/&#39;.$filename,&#39;w&#39;);
 fwrite($fpp1,$s) or die(&#39;写文件错误&#39;);
 echo &#39;加密成功!&#39;;
Copy after login

Method two:

<?php  
 function encode_file_contents($filename) {  
     $type=strtolower(substr(strrchr($filename,&#39;.&#39;),1));  
     if (&#39;php&#39; == $type && is_file($filename) && is_writable($filename)) { // 如果是PHP文件 并且可写 则进行压缩编码  
         $contents = file_get_contents($filename); // 判断文件是否已经被编码处理  
         $contents = php_strip_whitespace($filename);   
 
         // 去除PHP头部和尾部标识  
         $headerPos = strpos($contents,&#39;<?php&#39;);  
         $footerPos = strrpos($contents,&#39;?>&#39;);  
         $contents = substr($contents, $headerPos + 5, $footerPos - $headerPos);  
         $encode = base64_encode(gzdeflate($contents)); // 开始编码  
         $encode = &#39;<?php&#39;."\n eval(gzinflate(base64_decode("."&#39;".$encode."&#39;".")));\n\n?>";   
 
         return file_put_contents($filename, $encode);  
     }  
     return false;  
 }   
 
 //调用函数  
 $filename = &#39;dam.php&#39;;  
 encode_file_contents($filename);  
 echo "OK,加密完成!" 
 ?>
Copy after login

The above is the detailed content of How to implement source code encryption in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template