Table of Contents
1. Encryption method:
Generate TEST_ENCODE.PHP file after running encryption
Analysis
2. Decryption method
Home Backend Development PHP Tutorial PHP encryption method and decryption method

PHP encryption method and decryption method

May 15, 2018 am 11:27 AM
php method Decrypt

Test file

test.php

<?php    
    echo "hello world.";
?>
Copy after login

1. Encryption method:

<?php
/*
eval() 函数把字符串按照 PHP 代码来计算。该字符串必须是合法的 PHP 代码,且必须以分号结尾。
strtr() 字符替换 把字符串中的字符 "ia" 替换为 "eo":strtr("Hilla Warld","ia","eo");
*/
function T_rndstr($length = "") { //返回随机字符串
    $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    if ($length == "") {
        return str_shuffle($str);
    } else {
        return substr(str_shuffle($str), -$length);
    }
}

$T_k1 = T_rndstr(); //随机密匙1
$T_k2 = T_rndstr(); //随机密匙2
$vstr = file_get_contents("./test.php"); //要加密的文件
$v1 = base64_encode($vstr);
$c = strtr($v1, $T_k1, $T_k2); //根据密匙替换对应字符
$c = $T_k1 . $T_k2 . $c;
//$qn变量功能下面会讲解
$isqs = 3;
if ($isqs == "1") { // 1 取随机字符串为变量名
    $q = T_rndstr(); // 随机字符串
    $q1 = substr($q, 2, 3);
    $q2 = substr($q, 10, 10);
    $q3 = substr($q, 20, 12);
    $q4 = substr($q, 30, 10);
    $q5 = substr($q, 40, 8);
    $q6 = substr($q, 5, 5);
} else if ($isqs == "2") { // 2 为小写l和1组成,开头必须是字母
    $q1 = "ll11l1l1";
    $q2 = "l1lll11l";
    $q3 = "ll1l1lll";
    $q4 = "l1lll1l1";
    $q5 = "l1l1ll11";
    $q6 = "ll111l1l";
} else { // 大写O与数字0组成基本变量
    $q1 = "O00O0O";
    $q2 = "O0O000";
    $q3 = "O0OO00";
    $q4 = "OO0O00";
    $q5 = "OO0000";
    $q6 = "O00OO0";
}
$keystr = 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");
/* 字符串,里面必须要有变量所需的字符,比如 base64_decode
n1zb/ma5\vt0i28-pxuqy*6lrkdg9_ehcswo4+f37j
$q1 = base
$q3 = strtr
$q4 = substr
$q5 = 52 -> ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 长度
$q1 .= 64_decode -> base64_decode
*/
$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;;
echo $s;
file_put_contents(&#39;./test_encode.php&#39;, &#39;<?php &#39;.$s.&#39;?>&#39;);
?>
Copy after login

Generate TEST_ENCODE.PHP file after running encryption

<?php 
$O00OO0=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");
$O00O0O=$O00OO0{3}.$O00OO0{6}.$O00OO0{33}.$O00OO0{30};
$O0OO00=$O00OO0{33}.$O00OO0{10}.$O00OO0{24}.$O00OO0{10}.$O00OO0{24};
$OO0O00=$O0OO00{0}.$O00OO0{18}.$O00OO0{3}.$O0OO00{0}.$O0OO00{1}.$O00OO0{24};
$OO0000=$O00OO0{7}.$O00OO0{13};
$O00O0O.=$O00OO0{22}.$O00OO0{36}.$O00OO0{29}.$O00OO0{26}.$O00OO0{30}.$O00OO0{32}.$O00OO0{35}.$O00OO0{26}.$O00OO0{30};
eval($O00O0O("JE8wTzAwMD0iTGhxQk16QW5Zc29qUlVmSVhKZVBGSFF2Q2FwRWtOVFpHY2lXdWxtYmdkclZ5S3R3RFNPeER2Z2pick1mT1hreXVHaFlTVW5FcFF3b1B0Y2lLQ2VSTkhkWlZhVElGc0FMeGxxV3ptSkJFejlXdFFNQ1BkTUZZUGphTzJ2b1lQVWtSWkJYSXhqM0kzVVhSUDRkSlcwbFl6OCt6d2s9IjtldmFsKCc/PicuJE8wME8wTygkTzBPTzAwKCRPTzBPMDAoJE8wTzAwMCwkT08wMDAwKjIpLCRPTzBPMDAoJE8wTzAwMCwkT08wMDAwLCRPTzAwMDApLCRPTzBPMDAoJE8wTzAwMCwwLCRPTzAwMDApKSkpOw=="));
?>
Copy after login

Analysis

<?php
// 所有的$O00OO等等都是变量!!!

/*
将
%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%6
解码成
n1zb/ma5\vt0i28-pxuqy*6lrkdg9_ehcswo4+f37j
并赋值给$O00OO0
*/
$O00OO0=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");

// 从$O00OO0及其他变量字符串中获取对应字符后组成新的字符串,并赋值给对应的变量
// 例如 $O00OO0{3} 代表 n1zb/ma5\vt0i28-pxuqy*6lrkdg9_ehcswo4+f37j 字符串中的第三个字符,b
// $O00O0O = "base"
$O00O0O=$O00OO0{3}.$O00OO0{6}.$O00OO0{33}.$O00OO0{30};
// $O0OO00 = "strtr"
$O0OO00=$O00OO0{33}.$O00OO0{10}.$O00OO0{24}.$O00OO0{10}.$O00OO0{24};
// $OO0O00 = "substr"
$OO0O00=$O0OO00{0}.$O00OO0{18}.$O00OO0{3}.$O0OO00{0}.$O0OO00{1}.$O00OO0{24};
// $OO0000 = "52"
$OO0000=$O00OO0{7}.$O00OO0{13};
// $O00O0O = "base64_decode"
$O00O0O.=$O00OO0{22}.$O00OO0{36}.$O00OO0{29}.$O00OO0{26}.$O00OO0{30}.$O00OO0{32}.$O00OO0{35}.$O00OO0{26}.$O00OO0{30};

// 之后通过eval执行php代码
eval($O00O0O("JE8wTzAwMD0iTGhxQk16QW5Zc29qUlVmSVhKZVBGSFF2Q2FwRWtOVFpHY2lXdWxtYmdkclZ5S3R3RFNPeER2Z2pick1mT1hreXVHaFlTVW5FcFF3b1B0Y2lLQ2VSTkhkWlZhVElGc0FMeGxxV3ptSkJFejlXdFFNQ1BkTUZZUGphTzJ2b1lQVWtSWkJYSXhqM0kzVVhSUDRkSlcwbFl6OCt6d2s9IjtldmFsKCc/PicuJE8wME8wTygkTzBPTzAwKCRPTzBPMDAoJE8wTzAwMCwkT08wMDAwKjIpLCRPTzBPMDAoJE8wTzAwMCwkT08wMDAwLCRPTzAwMDApLCRPTzBPMDAoJE8wTzAwMCwwLCRPTzAwMDApKSkpOw=="));
/*
eval会执行后面的PHP代码
第一步为解码
$O00O0O 为 "base64_decode"。
即对后面的字符串进行base64解码
JE8wTzAwMD0iTGhxQk16QW5Zc29qUlVmSVhKZVBGSFF2Q2FwRWtOVFpHY2lXdWxtYmdkclZ5S3R3RFNPeER2Z2pick1mT1hreXVHaFlTVW5FcFF3b1B0Y2lLQ2VSTkhkWlZhVElGc0FMeGxxV3ptSkJFejlXdFFNQ1BkTUZZUGphTzJ2b1lQVWtSWkJYSXhqM0kzVVhSUDRkSlcwbFl6OCt6d2s9IjtldmFsKCc/PicuJE8wME8wTygkTzBPTzAwKCRPTzBPMDAoJE8wTzAwMCwkT08wMDAwKjIpLCRPTzBPMDAoJE8wTzAwMCwkT08wMDAwLCRPTzAwMDApLCRPTzBPMDAoJE8wTzAwMCwwLCRPTzAwMDApKSkpOw==
解码后
$O0O000="LhqBMzAnYsojRUfIXJePFHQvCapEkNTZGciWulmbgdrVyKtwDSOxDvgjbrMfOXkyuGhYSUnEpQwoPtciKCeRNHdZVaTIFsALxlqWzmJBEz9WtQMCPdMFYPjaO2voYPUkRZBXIxj3I3UXRP4dJW0lYz8+zwk=";
eval(&#39;?>&#39;.$O00O0O($O0OO00($OO0O00($O0O000,$OO0000*2),$OO0O00($O0O000,$OO0000,$OO0000),$OO0O00($O0O000,0,$OO0000))));

第二步是执行解码后的代码
解码后为另外一份代码,根据加密算法可知$O0O000变量的值为密钥1+密钥2+源代码编码并进行替换后的字符串,
这边先将替换掉的字符串还原回去,之后再进行base64_decode进行解码,即可获得原来的代码!!!
注意:由于源代码已经包含<?php ?>,所以这边必须加入&#39;?>&#39;
*/
?>
Copy after login

2. Decryption method

<?php
    // 加密的文件格式必须固定!!
    /*
$O00OO0=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");
$O00O0O=$O00OO0{3}.$O00OO0{6}.$O00OO0{33}.$O00OO0{30};
$O0OO00=$O00OO0{33}.$O00OO0{10}.$O00OO0{24}.$O00OO0{10}.$O00OO0{24};
$OO0O00=$O0OO00{0}.$O00OO0{18}.$O00OO0{3}.$O0OO00{0}.$O0OO00{1}.$O00OO0{24};
$OO0000=$O00OO0{7}.$O00OO0{13};
$O00O0O.=$O00OO0{22}.$O00OO0{36}.$O00OO0{29}.$O00OO0{26}.$O00OO0{30}.$O00OO0{32}.$O00OO0{35}.$O00OO0{26}.$O00OO0{30};
eval($O00O0O("JE8wTzAwMD0iTmV5SElCamZRdk......1UYVNuUUpnY21UYVVoOCtoTm89IjtldmFsKCc/=="));?>
    */
    // 要解密的文件,去掉php标识
    $encode_sourcecode = file_get_contents("./test_e.php");
    $encode_sourcecode = str_replace(&#39;<?php&#39;,&#39;&#39;, $encode_sourcecode);
    $encode_sourcecode = str_replace(&#39;?>&#39;,&#39;&#39;, $encode_sourcecode);
    // 提取第一次需要解密的内容
    // 即JE8wTzAwMD0iTmV5SElCamZRdk......1UYVNuUUpnY21UYVVoOCtoTm89IjtldmFsKCc/==
    echo &#39;</br></br>------------------------------$encode_sourcecode_content</br>&#39;;
    $start = strripos($encode_sourcecode, &#39;("&#39;) + 2;
    $end = strripos($encode_sourcecode, &#39;")&#39;);
    $encode_sourcecode_content = substr($encode_sourcecode, $start, $end - $start);
    echo $encode_sourcecode_content;
    // 解密加密部分的代码后的内容
    // $O0O000="NeyHIBjfQvDMwo......JoSptgMdl3M3JgSKQTiB0nuh8+hNo="; eval(&#39;......OO0000))));
    echo &#39;</br></br>------------------------------$decode_sourcecode_content</br>&#39;;
    $decode_sourcecode_content = base64_decode($encode_sourcecode_content);
    echo $decode_sourcecode_content;
    // 解密后还是一个加密的代码,需要再次解码,所以要再次提取需要被解密的内容出来
    // 即NeyHIBjfQvDMwo......JoSptgMdl3M3JgSKQTiB0nuh8+hNo=
    echo &#39;</br></br>------------------------------$decode_sourcecode_content_encode_content</br>&#39;;
    $start = stripos($decode_sourcecode_content, &#39;"&#39;) + 1;
    $end = strripos($decode_sourcecode_content, &#39;"&#39;) ;
    $decode_sourcecode_content_encode_content = substr($decode_sourcecode_content, $start, $end - $start);
    echo $decode_sourcecode_content_encode_content;
    // 根据加密规则,替换字符并解码,即可得到原文件
    echo &#39;</br></br>------------------------------$decode_sourcecode_content_encode_content</br>&#39;;
    $origin_content = base64_decode(strtr(
        substr($decode_sourcecode_content_encode_content, 104),
        substr($decode_sourcecode_content_encode_content, 52, 52),
        substr($decode_sourcecode_content_encode_content, 0, 52)));
    var_dump($origin_content);
    file_put_contents(&#39;./test_origin.php&#39;, $origin_content);
 ?>
Copy after login

The above is the detailed content of PHP encryption method and decryption method. 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 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles