search
HomeBackend DevelopmentPHP TutorialSummary of PHP source code encryption methods

This time I will bring you a summary of PHP's source code encryption methods. What are the precautions for PHP's source code encryption? . Here are practical cases, let's take a look.

Although sharing is a traditional virtue, sometimes we work hard to write some programs just to sell some small money, earn some hard work, and to prevent some unscrupulous people from reselling, so we have to improve our programs. Encryption, let’s introduce how to encrypt our PHP source code through PHP’s custom function.

The first method

<?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;?>'); 
   $contents = substr($contents, $headerPos + 5, $footerPos - $headerPos); 
   $encode = base64_encode(gzdeflate($contents)); // 开始编码 
   $encode = '<?php &#39;."\n eval(gzinflate(base64_decode("."&#39;".$encode."&#39;".")));\n\n?>"; 
   return file_put_contents($filename, $encode); 
  } 
  return false; 
 } 
 //调用函数 
 $filename = 'dam.php'; 
 encode_file_contents($filename); 
 echo "OK,加密完成!" 
 ?>

The second method

<?php  
 
 function RandAbc($length = "") { // 返回随机字符串 
  $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 
  return str_shuffle($str); 
 } 
 $filename = &#39;index.php&#39;; //要加密的文件 
 $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;?>\'.$'.$q1.'($'.$q3.'($'.$q4.'($'.$q2.',$'.$q5.'*2),$'.$q4.'($'.$q2.',$'.$q5.',$'.$q5.'),$'.$q4.'($'.$q2.',0,$'.$q5.'))));').'"));'; 
 $s = '<?php  &#39;."\n".$s."\n".&#39; ?>'; 
 //echo $s; 
 // 生成 加密后的PHP文件 
 $fpp1 = fopen('temp_'.$filename, 'w'); 
 fwrite($fpp1, $s) or die('写文件错误'); 
 
 ?>

The third method

<?php  
 class text_auth 
 { 
  var $n_iter; 
  function text_auth() 
  { 
   $this->setIter(32); 
  } 
  function setIter($n_iter) 
  { 
   $this->n_iter = $n_iter; 
  } 
  function getIter() 
  { 
   return $this->n_iter; 
  } 
  function encrypt($data, $key) 
  { 
   $n = $this->_resize($data, 4); 
   $data_long[0] = $n; 
   $n_data_long = $this->_str2long(1, $data, $data_long); 
   $n = count($data_long); 
   if (($n & 1) == 1) { 
    $data_long[$n] = chr(0); 
    $n_data_long++; 
   } 
   $this->_resize($key, 16, true); 
   if ( '' == $key ) 
    $key = '0000000000000000'; 
   $n_key_long = $this->_str2long(0, $key, $key_long); 
   $enc_data = ''; 
   $w   = array(0, 0); 
   $j   = 0; 
   $k   = array(0, 0, 0, 0); 
   for ($i = 0; $i _encipherLong($data_long[$i], $data_long[++$i], $w, $k); 
    $enc_data .= $this->_long2str($w[0]); 
    $enc_data .= $this->_long2str($w[1]); 
   } 
   return $enc_data; 
  } 
  function decrypt($enc_data, $key) 
  { 
   $n_enc_data_long = $this->_str2long(0, $enc_data, $enc_data_long); 
   $this->_resize($key, 16, true); 
   if ( '' == $key ) 
    $key = '0000000000000000'; 
   $n_key_long = $this->_str2long(0, $key, $key_long); 
   $data = ''; 
   $w  = array(0, 0); 
   $j  = 0; 
   $len = 0; 
   $k  = array(0, 0, 0, 0); 
   $pos = 0; 
   for ($i = 0; $i _decipherLong($enc_data_long[$i], $enc_data_long[$i + 1], $w, $k); 
    if (0 == $i) { 
     $len = $w[0]; 
     if (4 _long2str($w[1]); 
     } else { 
      $data .= substr($this->_long2str($w[1]), 0, $len % 4); 
     } 
    } else { 
     $pos = ($i - 1) * 4; 
     if ($pos + 4 _long2str($w[0]); 
 
      if ($pos + 8 _long2str($w[1]); 
      } elseif ($pos + 4 _long2str($w[1]), 0, $len % 4); 
      } 
     } else { 
      $data .= substr($this->_long2str($w[0]), 0, $len % 4); 
     } 
    } 
   } 
   return $data; 
  } 
  function _encipherLong($y, $z, &$w, &$k) 
  { 
   $sum = (integer) 0; 
   $delta = 0x9E3779B9; 
   $n  = (integer) $this->n_iter; 
   while ($n-- > 0) { 
    $y  = $this->_add($y, 
         $this->_add($z _rshift($z, 5), $z) ^ 
          $this->_add($sum, $k[$sum & 3])); 
    $sum = $this->_add($sum, $delta); 
    $z  = $this->_add($z, 
         $this->_add($y _rshift($y, 5), $y) ^ 
          $this->_add($sum, $k[$this->_rshift($sum, 11) & 3])); 
   } 
   $w[0] = $y; 
   $w[1] = $z; 
  } 
  function _decipherLong($y, $z, &$w, &$k) 
  { 
   $sum = 0xC6EF3720; 
   $delta = 0x9E3779B9; 
   $n  = (integer) $this->n_iter; 
 
   while ($n-- > 0) { 
    $z  = $this->_add($z, 
         -($this->_add($y _rshift($y, 5), $y) ^ 
           $this->_add($sum, $k[$this->_rshift($sum, 11) & 3]))); 
    $sum = $this->_add($sum, -$delta); 
    $y  = $this->_add($y, 
         -($this->_add($z _rshift($z, 5), $z) ^ 
           $this->_add($sum, $k[$sum & 3]))); 
   } 
   $w[0] = $y; 
   $w[1] = $z; 
  } 
  function _resize(&$data, $size, $nonull = false) 
  { 
   $n  = strlen($data); 
   $nmod = $n % $size; 
   if ( 0 == $nmod ) 
    $nmod = $size; 
   if ($nmod > 0) { 
    if ($nonull) { 
     for ($i = $n; $i  $integer) { 
    $integer = fmod($integer, 0xffffffff + 1); 
   } 
   if (0x7fffffff  $integer) { 
    $integer += 0xffffffff + 1.0; 
   } 
   if (0 > $integer) { 
    $integer &= 0x7fffffff; 
    $integer >>= $n; 
    $integer |= 1 >= $n; 
   } 
   return $integer; 
  } 
  function _add($i1, $i2) 
  { 
   $result = 0.0; 
   foreach (func_get_args() as $value) { 
    if (0.0 > $value) { 
     $value -= 1.0 + 0xffffffff; 
    } 
    $result += $value; 
   } 
   if (0xffffffff  $result) { 
    $result = fmod($result, 0xffffffff + 1); 
   } 
   if (0x7fffffff  $result) { 
    $result += 0xffffffff + 1.0; 
   } 
   return $result; 
  } 
 } 
 ?> 
使用方法参考如下:
// 加密过程
view sourceprint?
 $text_file = S_ROOT . './456.php'; 
 $str = @file_get_contents($text_file); 
 require_once S_ROOT . "./text_auth.php"; 
 $text_auth = new text_auth(64); 
 $str = $text_auth->encrypt($str, "qianyunlai.com"); 
 $filename = S_ROOT . './789.php'; // 加密后的文本为二进制,普通的文本编辑器无法正常查看 
 file_put_contents($filename, $str); 
// 解密过程
view sourceprint
?01 $text_file = S_ROOT . './789.php'; 
 $str = @file_get_contents($text_file); 
 require_once S_ROOT . "./text_auth.php"; 
 $text_auth = new text_auth(64); 
 $str = $text_auth->decrypt($str, "qianyunlai.com"); 
 $filename = S_ROOT . './456.php'; 
 file_put_contents($filename, $str);

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use PHP unlink and rmdir to delete files in the directory

php and js open a local exe application to pass parameters Detailed explanation of steps

The above is the detailed content of Summary of PHP source code encryption methods. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools