


How is the internal algorithm of PHP encryption and decryption implemented?
When I was learning about URL jump recently, I learned three super easy-to-use PHP encryption and decryption functions, which seem to be in discuz... The reason for using these encryption and decryption is because sometimes someone wants to crack your URL address after it is obtained. The content of the value must know your key. Without the key, it will take a while to know the content of your URL.
Package them into a file and call it fun.php
The code is as follows:
<?php function passport_encrypt($txt, $key) { srand((double) microtime () * 1000000); $encrypt_key = md5(rand(0, 32000)); $ctr = 0; $tmp = ''; for($i = 0;$i < strlen($txt); $i++) { $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr; $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]); } return base64_encode(passport_key($tmp, $key)); } function passport_decrypt($txt, $key) { $txt = passport_key(base64_decode($txt), $key); $tmp = ''; for($i = 0;$i < strlen($txt); $i++) { $md5 = $txt[$i]; $tmp .= $txt[++$i] ^ $md5; } return $tmp; } function passport_key($txt, $encrypt_key) { $encrypt_key = md5($encrypt_key); $ctr = 0; $tmp = ''; for($i = 0; $i < strlen($txt); $i++) { $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr; $tmp .= $txt[$i] ^ $encrypt_key[$ctr++]; } return $tmp; } ?>
The following are some examples to deepen the understanding of these three encryption and decryption functions. Understand the
code as follows:
//string.php <?php include “fun.php”; $txt = “This is a test”; $key = “testkey”; $encrypt = passport_encrypt($txt,$key); $decrypt = passport_decrypt($encrypt,$key); echo $txt.”<br><hr>”; echo $encrypt.”<br><hr>”; echo $decrypt.”<br><hr>”; ?> //array.php <?php include “fun.php”; $array = array( "a" => "1", "b" => "2", "c" => "3", "d" => "4" ); //serialize产生一个可存储的值,返回一个 字符串 ,unserialize还原 $txt = serialize($array); $key = “testkey”; $encrypt = passport_encrypt($txt,$key); $decrypt = passport_decrypt($encrypt,$key); $decryptArray = unserialize($decrypt); echo $txt.”<br><hr>”; echo $encrypt.”<br><hr>”; echo $decrypt.”<br><hr>”; echo $decryptArray.”<br><hr>”; ?>
The key point is when you want to jump to another URL, but you must ensure that your session is correct Sometimes, you need to do something with the session. It seems that a company has a website and a forum, and there are registration and login in both places, but you don’t want the session to expire when the user jumps to the forum after logging in on the homepage, that is, Log in once and run the entire company
How to handle the user's session
Web pages are stateless. If you want to continue to use the session in a new web page, you need to change the session from Moving one place to another, some people may have thought that I can call it by passing the URL. PHP has a variable for processing sessions, called $_SESSION. So the session that needs to be registered is converted into an array. Well. Then, you can write like this:
The code is as follows:
//login.php <?php session_start(); include “fun.php”; $_SESSION[“userid”]; $_SESSION[“username”]; $_SESSION[“userpwd”]; header("Location: http://$domain/process.php?s=".urlencode(passport_encrypt(serialize($_SESSION),"sessionkey"))); ?>
In the above example, serialize is first used to turn $_SESSION into storable data, and then the data is converted into storable data through passport_encrypt. The reason for adding urlencode to encryption is because when $_SESSION is encrypted, unexpected encoding may occur, so just in case (it turns out to be very effective)
Let’s deal with it first
The code is as follows:
//process.php <?php session_start(); include “fun.php”; $_SESSION=unserialize(passport_decrypt($_GET["s"],"sessionkey")); header("Location: http://$domain/index.php"); ?>
First use $_GET["s"] to obtain the parameters of the URL, then use passport_decrypt to decrypt it, and then use unserialize to restore the data to the original data. At this step After processing, your web page can jump freely through the header.
This method also involves security issues. If your url address is obtained by others during the address transmission process, it would be really embarrassing. Although they may not be able to decipher the content in the url. , but people can also directly use this URL address to log in to some of your personal accounts, email accounts, and even bank accounts (of course, few people will write like this, I am an exception, haha). It sounds scary. But in fact, you can jump Transfer the page to cancel the session.
The following is the enhanced version of process.php
The code is as follows:
<?php session_start(); include_once"fun.php"; $_SESSION=unserialize(passport_decrypt($_GET["s"],"sessionkey")); if((time()-$_SESSION["TIME"])>30){ header("Location: http://$domain/ login.php"); unset($_SESSION["USERNAME"]); unset($_SESSION["PASSWORD"]); } else header("Location: http://$domain/ index.php"); ?>
Before writing this file, you must log in Set there
$_SESSION["TIME"] = time();
The reason for setting this is mainly to get the time on both sides. If the jump time exceeds 30 seconds, you You can make it jump to the login.php login page. Customers with slow Internet speeds will be embarrassed, but this also prevents the problem if this URL is obtained by someone and the person does not log in within 30 seconds. Ah, log in again after timeout.
$_SESSION["USERNAME"] and $_SESSION["PASSWORD"] are the username and password that user needs to enter when logging in. The reason for canceling these two sessions is that if your URL is obtained by someone, although that person will jump to the login.php page within more than 30 seconds, those passed sessions are still valid, as long as the URL suffix is login. php is changed to index.php. Then he can log in successfully.
The above is the detailed content of How is the internal algorithm of PHP encryption and decryption implemented?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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,

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

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 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.
