


A set of PHP reversible encryption and decryption algorithm example codes
For most password encryption, we can use md5, sha1 and other methods. Data leakage can be effectively prevented, but these methods only apply to data encryption that does not require restoration.
For information that needs to be restored, a reversible encryption and decryption algorithm needs to be used.
The following set of PHP functions is the method to implement this encryption and decryption:
The encryption algorithm is as follows:
function encrypt($data, $key) { $key = md5($key); $x = 0; $len = strlen($data); $l = strlen($key); for ($i = 0; $i < $len; $i++) { if ($x == $l) { $x = 0; } $char .= $key{$x}; $x++; } for ($i = 0; $i < $len; $i++) { $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256); } return base64_encode($str); }
The decryption algorithm is as follows:
function decrypt($data, $key) { $key = md5($key); $x = 0; $data = base64_decode($data); $len = strlen($data); $l = strlen($key); for ($i = 0; $i < $len; $i++) { if ($x == $l) { $x = 0; } $char .= substr($key, $x, 1); $x++; } for ($i = 0; $i < $len; $i++) { if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) { $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); } else { $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); } } return $str; }
The above encryption and decryption processes all require the use of an encryption key (i.e. parameter $key) .
$data = 'PHP加密解密算法'; // 被加密信息 $key = '123'; // 密钥 $encrypt = encrypt($data, $key); $decrypt = decrypt($encrypt, $key); echo $encrypt, "\n", $decrypt;
The above will output results similar to the following:
gniCSOzZG+HnS9zcFea7SefNGhXF
PHP encryption and decryption algorithm
As can be seen from the above results, this is a set of reversible encryption and decryption algorithms that can be used to encrypt some data that needs to be restored.
For more articles related to a set of PHP reversible encryption and decryption algorithm example codes, please pay attention to 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

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

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



Title: Data Encryption and Decryption Using ReactQuery and Database Introduction: This article will introduce how to use ReactQuery and database for data encryption and decryption. We will use ReactQuery as the data management library and combine it with the database to perform data encryption and decryption operations. By combining these two technologies, we can securely store and transmit sensitive data, and perform encryption and decryption operations when needed to ensure data security. Text: 1. ReactQue

As WeChat mini programs become more popular in the mobile application market, their development has also received more and more attention. In small programs, PHP, as a commonly used back-end language, is often used to handle the encryption and decryption of sensitive data. This article will introduce how to use PHP to implement encryption and decryption in WeChat applet. 1. What are encryption and decryption? Encryption is the conversion of sensitive data into an unreadable form to ensure that the data is not stolen or tampered with during transmission. Decryption is the restoration of encrypted data to original data. In small programs, encryption and decryption usually include

Encrypting and decrypting sensitive data using Yii framework middleware Introduction: In modern Internet applications, privacy and data security are very important issues. To ensure that users' sensitive data is not accessible to unauthorized visitors, we need to encrypt this data. The Yii framework provides us with a simple and effective way to implement the functions of encrypting and decrypting sensitive data. In this article, we’ll cover how to achieve this using the Yii framework’s middleware. Introduction to Yii framework Yii framework is a high-performance PHP framework.

This article is a tutorial on how to encrypt and decrypt files without using third-party encryption and decryption tools. A tutorial on adding encryption and decryption methods to the right-click menu of Win11. Since the registry needs to be modified, you must make a backup before proceeding. operate. 1. First, press the [Win+R] key combination on the keyboard to open Run, then enter the [regedit] command and press [OK or Enter] to open the Registry Editor; 2. In the User Account Control window, you need to allow Does this app make changes to your device? Click [Yes]; 3. Registry Editor window, expand to the following path: HKEY_CURRENT_USER\Software\Microsoft\Windows\Curr

Introduction to data encryption and decryption examples in the process of docking with the API interface of Tencent Cloud Server using PHP: In the process of docking with the API interface of Tencent Cloud Server, the security of data is very important. In order to ensure the security of data during transmission and storage, we need to encrypt sensitive information. This article will introduce how to use PHP to encrypt and decrypt data to improve data confidentiality and integrity. Data encryption: When making API requests, we need to encrypt sensitive information to ensure data security. common

PHP mailbox development: realizing email encryption and decryption functions. With the increasing development of information transmission, email has become one of the important communication methods for people. However, the ensuing security issues have gradually attracted people's attention. In order to protect the security of emails, encryption and decryption have become important aspects of sending and receiving emails. This article will introduce how to use PHP to develop email encryption and decryption functions to improve email security. 1. Principle and function of encryption Email encryption is to convert the email content using a specific algorithm so that in addition to the recipient

The Java function library provides a wealth of encryption and decryption tools, including JCE, JCA, Apache CommonsCrypt, etc. JCE provides encryption algorithms and functions, JCA provides interfaces to access encryption service providers, and Apache CommonsCrypt contains more encryption algorithms and tools. The practical case shows how to use JCE to AES encrypt text and generate Base64-encoded ciphertext.

How to use Go's SectionReader module to encrypt and decrypt the content of a specified part of a file? Introduction: In development, file content encryption and decryption is a very common requirement. The Go language provides a wealth of libraries and modules to meet this need. Among them, SectionReader is a very practical module that allows us to specify the range of content in a large file and perform reading, encryption, and decryption operations. This article will introduce how to use Go’s SectionReader module to implement
