Home Backend Development PHP Tutorial Summary of methods for encrypting source code in PHP

Summary of methods for encrypting source code in PHP

Feb 06, 2018 pm 03:48 PM
php Summarize source code

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 encrypt our programs, as follows Let's introduce how to encrypt our PHP source code through PHP's custom function. This article mainly shares with you a summary of several methods for encrypting source code in PHP (recommended). It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.

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,加密完成!" 
 ?>
Copy after login

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('写文件错误'); 
 
 ?>
Copy after login

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 < $n_data_long; ++$i) { 
    if ($j + 4 <= $n_key_long) { 
     $k[0] = $key_long[$j]; 
     $k[1] = $key_long[$j + 1]; 
     $k[2] = $key_long[$j + 2]; 
     $k[3] = $key_long[$j + 3]; 
    } else { 
     $k[0] = $key_long[$j % $n_key_long]; 
     $k[1] = $key_long[($j + 1) % $n_key_long]; 
     $k[2] = $key_long[($j + 2) % $n_key_long]; 
     $k[3] = $key_long[($j + 3) % $n_key_long]; 
    } 
    $j = ($j + 4) % $n_key_long; 
 
    $this->_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 < $n_enc_data_long; $i += 2) { 
    if ($j + 4 <= $n_key_long) { 
     $k[0] = $key_long[$j]; 
     $k[1] = $key_long[$j + 1]; 
     $k[2] = $key_long[$j + 2]; 
     $k[3] = $key_long[$j + 3]; 
    } else { 
     $k[0] = $key_long[$j % $n_key_long]; 
     $k[1] = $key_long[($j + 1) % $n_key_long]; 
     $k[2] = $key_long[($j + 2) % $n_key_long]; 
     $k[3] = $key_long[($j + 3) % $n_key_long]; 
    } 
    $j = ($j + 4) % $n_key_long; 
 
    $this->_decipherLong($enc_data_long[$i], $enc_data_long[$i + 1], $w, $k); 
 
    if (0 == $i) { 
     $len = $w[0]; 
     if (4 <= $len) { 
      $data .= $this->_long2str($w[1]); 
     } else { 
      $data .= substr($this->_long2str($w[1]), 0, $len % 4); 
     } 
    } else { 
     $pos = ($i - 1) * 4; 
     if ($pos + 4 <= $len) { 
      $data .= $this->_long2str($w[0]); 
 
      if ($pos + 8 <= $len) { 
       $data .= $this->_long2str($w[1]); 
      } elseif ($pos + 4 < $len) { 
       $data .= substr($this->_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 << 4 ^ $this->_rshift($z, 5), $z) ^ 
          $this->_add($sum, $k[$sum & 3])); 
    $sum = $this->_add($sum, $delta); 
    $z  = $this->_add($z, 
         $this->_add($y << 4 ^ $this->_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 << 4 ^ $this->_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 << 4 ^ $this->_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 < $n - $nmod + $size; ++$i) { 
      $data[$i] = $data[$i % $n]; 
     } 
    } else { 
     for ($i = $n; $i < $n - $nmod + $size; ++$i) { 
      $data[$i] = chr(0); 
     } 
    } 
   } 
   return $n; 
  } 
 
  function _hex2bin($str) 
  { 
   $len = strlen($str); 
   return pack(&#39;H&#39; . $len, $str); 
  } 
 
  function _str2long($start, &$data, &$data_long) 
  { 
   $n = strlen($data); 
 
   $tmp = unpack(&#39;N*&#39;, $data); 
   $j  = $start; 
 
   foreach ($tmp as $value) 
    $data_long[$j++] = $value; 
 
   return $j; 
  } 
 
  function _long2str($l) 
  { 
   return pack(&#39;N&#39;, $l); 
  } 
 
  function _rshift($integer, $n) 
  { 
   if (0xffffffff < $integer || -0xffffffff > $integer) { 
    $integer = fmod($integer, 0xffffffff + 1); 
   } 
 
   if (0x7fffffff < $integer) { 
    $integer -= 0xffffffff + 1.0; 
   } elseif (-0x80000000 > $integer) { 
    $integer += 0xffffffff + 1.0; 
   } 
 
   if (0 > $integer) { 
    $integer &= 0x7fffffff; 
    $integer >>= $n; 
    $integer |= 1 << (31 - $n); 
   } else { 
    $integer >>= $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 || -0xffffffff > $result) { 
    $result = fmod($result, 0xffffffff + 1); 
   } 
 
   if (0x7fffffff < $result) { 
    $result -= 0xffffffff + 1.0; 
   } elseif (-0x80000000 > $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);
Copy after login

Related recommendations:

php Method to implement source code encryption

How to encrypt PHP source code? Solution to PHP binary encryption and decryption_PHP tutorial

php method to implement source code encryption, php source code encryption_PHP tutorial

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

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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles