Home Backend Development PHP Tutorial A set of PHP reversible encryption and decryption algorithm example codes

A set of PHP reversible encryption and decryption algorithm example codes

Dec 21, 2016 am 09:46 AM
encrypt and decode

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);
}
Copy after login

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;
}
Copy after login

The above encryption and decryption processes all require the use of an encryption key (i.e. parameter $key) .

$data = &#39;PHP加密解密算法&#39;;  // 被加密信息
$key = &#39;123&#39;;     // 密钥
$encrypt = encrypt($data, $key);
$decrypt = decrypt($encrypt, $key);
echo $encrypt, "\n", $decrypt;
Copy after login

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!


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)

Data encryption and decryption using React Query and database Data encryption and decryption using React Query and database Sep 26, 2023 pm 12:53 PM

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

Encryption and decryption implementation method developed in PHP in WeChat applet Encryption and decryption implementation method developed in PHP in WeChat applet Jun 01, 2023 am 08:12 AM

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

Encrypt and decrypt sensitive data using Yii framework middleware Encrypt and decrypt sensitive data using Yii framework middleware Jul 28, 2023 pm 07:12 PM

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.

How to add Win11 encryption function to the right-click menu? How to add Win11 encryption and decryption right-click shortcut How to add Win11 encryption function to the right-click menu? How to add Win11 encryption and decryption right-click shortcut Jan 07, 2024 am 08:45 AM

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

Example of data encryption and decryption during PHP Tencent Cloud Server API interface docking process Example of data encryption and decryption during PHP Tencent Cloud Server API interface docking process Jul 06, 2023 am 10:52 AM

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: implementing email encryption and decryption functions PHP mailbox development: implementing email encryption and decryption functions Sep 12, 2023 am 10:40 AM

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

What are the commonly used encryption and decryption tools in Java function libraries? What are the commonly used encryption and decryption tools in Java function libraries? May 05, 2024 am 08:54 AM

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? How to use Go's SectionReader module to encrypt and decrypt the content of a specified part of a file? Jul 23, 2023 am 08:24 AM

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

See all articles