Home Backend Development PHP Tutorial Complete tutorial: How to encrypt and decrypt using php extension MCrypt

Complete tutorial: How to encrypt and decrypt using php extension MCrypt

Jul 28, 2023 pm 12:25 PM
mcrypt php extension encrypt and decode

Complete tutorial: How to use PHP extension MCrypt for encryption and decryption

Introduction
In modern network applications, data confidentiality and security are particularly important. In order to ensure the security of data transmission and storage, encryption technology has become an essential tool. In PHP, the MCrypt extension provides an easy way to encrypt and decrypt data. This tutorial will show you how to encrypt and decrypt using the PHP extension MCrypt.

Step 1: Install MCrypt extension
MCrypt extension is a PHP extension used to encrypt and decrypt data. To use the MCrypt extension, you first need to install it on your server. You can install the MCrypt extension by following these steps:

  1. Check whether your PHP installation already supports the MCrypt extension. You can use the following command in the terminal or command line to check:

    php -m | grep mcrypt

    If the word "mcrypt" is displayed, the MCrypt extension has been installed and you can skip this step . Otherwise, the MCrypt extension needs to be installed.

  2. Use a package management tool (such as apt, yum or brew) to install the MCrypt extension. Here are the commands for installing MCrypt extensions with several common package management tools:

    Using apt (for Debian or Ubuntu):

    sudo apt-get install php-mcrypt

    Use yum (for CentOS or Fedora):

    sudo yum install php-mcrypt

    Use brew (for macOS):

    brew install mcrypt

    After the installation is complete, restart your web server.

Step Two: Encrypt Data
Once the MCrypt extension is installed on your server, you can start using it to encrypt data. The following is an example of a basic encryption function:

function encryptData($data, $key, $iv) {
    $cipher = MCRYPT_RIJNDAEL_128;
    $mode = MCRYPT_MODE_CBC;
    $padding = 16 - (strlen($data) % 16);
    $data = $data . str_repeat(chr($padding), $padding);
    $encryptedData = mcrypt_encrypt($cipher, $key, $data, $mode, $iv);
    return base64_encode($encryptedData);
}
Copy after login

In the above example, we defined a function called "encryptData" that accepts three parameters: the data to be encrypted, the key, and the initialization vector (IV).

In the encryption function, we used the Rijndael 128-bit encryption algorithm (also known as AES) and CBC mode (key block chaining) in MCrypt. We use a data block size of 16 bytes (128 bits) and pad at the end of the data to ensure that the length of the encrypted data is a multiple of 16.

Use the mcrypt_encrypt function to encrypt the data, and use base64_encode to encode the encrypted data. Returns the encoded data.

Step Three: Decrypt Data
Once the data is encrypted, it needs to be decrypted to restore the original data. The following is a basic decryption function example:

function decryptData($data, $key, $iv) {
    $cipher = MCRYPT_RIJNDAEL_128;
    $mode = MCRYPT_MODE_CBC;
    $decryptedData = mcrypt_decrypt($cipher, $key, base64_decode($data), $mode, $iv);
    $padding = ord($decryptedData[strlen($decryptedData) - 1]);
    return substr($decryptedData, 0, -$padding);
}
Copy after login

In this decryption function example, we also use the Rijndael 128-bit encryption algorithm and CBC mode.

Use the mcrypt_decrypt function to decrypt the data and use base64_decode to decode the incoming data.

We also need to get the number of padding bytes of the decrypted data and use the substr function to truncate the padding part.

Conclusion
Congratulations! You have completed the tutorial on how to encrypt and decrypt via the MCrypt extension. Encryption is an important security measure when it comes to protecting sensitive data. With the appropriate use of the MCrypt extension, you can easily add encryption and decryption functionality to your PHP applications. Remember to save your key and IV value to ensure that when decrypting the data you can correctly recover the original data.

The above is the detailed content of Complete tutorial: How to encrypt and decrypt using php extension MCrypt. 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)

How to check which extensions are used in php How to check which extensions are used in php Aug 01, 2023 pm 04:13 PM

You can check which extensions are used by PHP by viewing the phpinfo() function output, using command line tools, and checking the PHP configuration file. 1. View the phpinfo() function output, create a simple PHP script, save this script as phpinfo.php, and upload it to your web server. Access this file in the browser and use the browser's search function. Just look for the keyword "extension" or "extension_loaded" on the page to find information about the extension.

How to use php to extend PDO to connect to Oracle database How to use php to extend PDO to connect to Oracle database Jul 29, 2023 pm 07:21 PM

How to use PHP to extend PDO to connect to Oracle database Introduction: PHP is a very popular server-side programming language, and Oracle is a commonly used relational database management system. This article will introduce how to use PHP extension PDO (PHPDataObjects) to connect to Oracle database. 1. Install the PDO_OCI extension. To connect to the Oracle database, you first need to install the PDO_OCI extension. Here are the steps to install the PDO_OCI extension: Make sure

How to replace mcrypt in php How to replace mcrypt in php Oct 31, 2022 am 09:46 AM

How to replace mcrypt with php: 1. Open the corresponding php file; 2. Find the original encryption and decryption code; 3. Use the "openssl_encrypt" and "openssl_decrypt" methods to replace it.

How to extend SuiteCRM's report generation capabilities using PHP How to extend SuiteCRM's report generation capabilities using PHP Jul 19, 2023 am 10:27 AM

How to use PHP to extend the report generation function of SuiteCRM SuiteCRM is a powerful open source CRM system that provides rich functions to help enterprises manage customer relationships. One of the important functions is report generation. Using reports can help enterprises better understand their business situations and make correct decisions. This article will introduce how to use PHP to extend the report generation function of SuiteCRM and provide relevant code examples. Before starting, you need to make sure that SuiteCRM is installed.

Getting Started with PHP: PHP Extension Installation Getting Started with PHP: PHP Extension Installation May 20, 2023 am 08:49 AM

When developing with PHP, we may need to use some PHP extensions. These extensions can provide us with more functions and tools, making our development work more efficient and convenient. But before using these extensions, we need to install them first. This article will introduce you to how to install PHP extensions. 1. What is a PHP extension? PHP extensions refer to components that provide additional functionality and services to the PHP programming language. These components can be installed and used through PHP's extension mechanism. PHP extension can help us with

How to use PHP's geoip extension? How to use PHP's geoip extension? Jun 01, 2023 am 09:13 AM

PHP is a popular server-side scripting language that can handle dynamic content on web pages. The geoip extension for PHP allows you to get information about the user's location in PHP. In this article, we’ll cover how to use PHP’s geoip extension. What is the GeoIP extension for PHP? The geoip extension for PHP is a free, open source extension that allows you to obtain data about IP addresses and location information. This extension can be used with the GeoIP database, a database developed by MaxMin

PHP extension and PHP version management of Pagoda Panel PHP extension and PHP version management of Pagoda Panel Jun 21, 2023 am 08:49 AM

Pagoda Panel is an open source server management panel. While providing website operators with convenient website management, database management, SSL certificate management and other services, it also provides powerful PHP extension and PHP version management functions, making server management easier. Be more simple and efficient. 1. PHP extension PHP extension is a module used to enhance PHP functions. By installing PHP extensions, more functions and services can be implemented, such as: accelerator: accelerator can significantly improve PHP performance, and reduce service load by caching PHP scripts.

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

See all articles