search
HomeBackend DevelopmentPHP TutorialIntroduction to how to enable, encrypt and decrypt php mcrypt

  1. $str = "I am Li Yun";
  2. $key = "123qwe.019860905061X";
  3. $cipher = MCRYPT_RIJNDAEL_128;
  4. $mode = MCRYPT_MODE_ECB;
  5. $iv = mcrypt_creat e_iv(mcrypt_get_iv_size( $cipher,$mode),MCRYPT_RAND);
  6. echo "Original text:".$str."
    ";
  7. $str_encrypt = mcrypt_encrypt($cipher,$key,$str,$mode,$iv);
  8. echo "The encrypted content is: ".$str_encrypt."
    ";
  9. $str_decrypt = mcrypt_decrypt($cipher,$key,$str_encrypt,$mode,$iv);
  10. echo "After decryption Content: ".$str_decrypt."
    ";
  11. ?>
Copy the code

Run the results: Original text: I am Li Yun The encrypted content is: B @鴹?=(I褣Z% Decrypted content: I am Li Yun

1) Before using the PHP encryption extension library Mcrypt to encrypt and decrypt data, first create an initialization vector, referred to as iv for short. From $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND); it can be seen that creating an initialization vector requires two parameters: size specifies the size of iv; source is the source of iv, where the value MCRYPT_RAND is the system random number.

2) The function mcrypt_get_iv_size($cipher,$modes) returns the initialization vector size. The parameters cipher and mode refer to the algorithm and encryption mode respectively.

3), encryption function $str_encrypt = mcrypt_encrypt($cipher,$key,$str,$modes,$iv); The five parameters of this function are as follows: cipher——encryption algorithm, key——key, data( str)——data that needs to be encrypted, mode——algorithm mode, iv——initialization vector

4), decryption function mcrypt_decrypt($cipher,$key,$str_encrypt,$modes,$iv); The parameters of this function and the encryption function are almost the same. The only difference is data, which means data is the data that needs to be decrypted$ str_encrypt, not the raw data $str.

//Writing in the manual:

  1. //Specify the size of the initialization vector iv:
  2. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  3. //Create the initialization vector:
  4. $iv = mcrypt_create_iv($iv_size, MCRY PT_RAND);
  5. //Encrypted password:
  6. $key = "123qwe.019860905061x";
  7. //Original content (unencrypted):
  8. $text = "My name is Adam Li!";
  9. echo $text. "
    n ";
  10. //Encrypted content:
  11. $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
  12. echo $crypttext. "n
    ";
  13. //Decrypt the encrypted content :
  14. $str_decrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
  15. echo $str_decrypt;
  16. //Original link http://bbs.it-home.org/article/7382.html
  17. ? >
Copy code

Example of encryption/decryption request:

  1. $request_params = array(
  2. 'controller' => 'todo',
  3. 'action' => 'read',
  4. 'username' => "bl",
  5. 'userpass' => "a1"
  6. );
  7. $private_key = "28e336ac6c9423d946ba02d19c6a2632";
  8. //encrypt request
  9. $enc_request = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $ private_key, json_encode($request_params), MCRYPT_MODE_ECB));
  10. echo "CRYPT:".$enc_request."
    ";
  11. //decrypt request
  12. $params = json_decode(trim(mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $private_key, base64_decode($enc_request), MCRYPT_MODE_ECB )), true);
  13. echo "ENCRYPT:
    ";
  14. //print result
  15. var_dump($params);
  16. ?>
Copy code

Note: The parameters cipher, key and mode in the encryption and decryption functions must correspond one to one, otherwise the data cannot be restored.



Statement
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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Discover File Downloads in Laravel with Storage::downloadDiscover File Downloads in Laravel with Storage::downloadMar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

How to Register and Use Laravel Service ProvidersHow to Register and Use Laravel Service ProvidersMar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.