search
HomeBackend DevelopmentPHP TutorialShare installation and application tips of PHP encryption extension library Mcrypt_PHP tutorial

Share installation and application tips of PHP encryption extension library Mcrypt_PHP tutorial

Jul 15, 2016 pm 01:33 PM
mcryptphpshareFunctionencryptionencrypt and decodeCanInstallaccomplishLibraryApplication tipsExpand

Among them, the Mcrypt extension library can implement encryption and decryption functions, that is, it can encrypt plain text and restore cipher text.

1. PHP encryption extension library Mcrypt installation

Mrcypt is not installed during the standard PHP installation process, but libmcrypt is included in the main directory of PHP. dll and libmhash.dll files (libmhash.dll is the Mhash extension library and can be installed together here). First, copy these two files to the system directory windowssystem32, then press the Ctrl+F shortcut key in the PHP.ini file to pop up the search box, and find; extension=php-mcrypt.dll and; extension=php_mhash.dll statement, then remove the preceding ";"; finally, save and restart the Apache server to take effect.

2. Algorithms and encryption modes of the PHP encryption extension library Mcrypt

The Mcrypt library supports more than 20 encryption algorithms and 8 encryption modes. You can use the function mcrypt_list_algorithms( ) and mcrypt_list_modes() to display, the results are as follows:

The algorithms supported by Mcrypt are: cast-128 gost rijndael-128 twofish arcfour cast-256 loki97 rijndael-192 saferplus wake blowfish-compat des rijndael-256 serpent xtea blowfish enigma rc2 tripledes

The encryption modes supported by Mcrypt are: cbc cfb ctr ecb ncfb nofb ofb stream

These algorithms and modes should be expressed as constants in the application, and prefixed when writing MCRYPT_ and MCRYPT_, as shown in the following Mcrypt application example:

DES algorithm is represented as MCRYPT_DES;
ECB mode is represented as MCRYPT_MODE_ECB;

3.PHP encryption extension Library Mcrypt application

First look at an example to understand the workflow of Mcrypt, and then look at the functions used in some processes:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li>
<span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">"我的名字是?一般人我不告诉他!"</span><span>;   </span>
</li>
<li class="alt"><span>//加密内容  </span></li>
<li>
<span>$</span><span class="attribute">key</span><span> = </span><span class="attribute-value">"key:111"</span><span>;   </span>
</li>
<li class="alt"><span>//密钥  </span></li>
<li>
<span>$</span><span class="attribute">cipher</span><span> = </span><span class="attribute-value">MCRYPT_DES</span><span>;  </span>
</li>
<li class="alt"><span>//密码类型  </span></li>
<li>
<span>$</span><span class="attribute">modes</span><span> = </span><span class="attribute-value">MCRYPT_MODE_ECB</span><span>;  </span>
</li>
<li class="alt"><span>//密码模式  </span></li>
<li>
<span>$</span><span class="attribute">iv</span><span> = </span><span class="attribute-value">mcrypt_create_iv</span><span>(mcrypt_get_iv_size<br>($cipher,$modes),MCRYPT_RAND);//初始化向量  </span>
</li>
<li class="alt">
<span>echo "加密明文:".$str."</span><span class="tag"><span class="tag-name">p</span><span class="tag">></span><span>";  </span></span>
</li>
<li>
<span>$</span><span class="attribute">str_encrypt</span><span> = </span><span class="attribute-value">mcrypt_encrypt</span><span>($cipher,<br>$key,$str,$modes,$iv);  </span>
</li>
<li class="alt"><span>//加密函数  </span></li>
<li>
<span>echo "加密密文:".$str_encrypt." </span><span class="tag"><span class="tag-name">p</span><span class="tag">></span><span>";  </span></span>
</li>
<li class="alt">
<span>$</span><span class="attribute">str_decrypt</span><span> = </span><span class="attribute-value">mcrypt_decrypt</span><span>($cipher,<br>$key,$str_encrypt,$modes,$iv);  </span>
</li>
<li><span>//解密函数  </span></li>
<li class="alt"><span>echo "还原:".$str_decrypt;  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>

Run results:

Encrypted plaintext: What is my name? I don’t tell most people!
Encrypted ciphertext: 锍 Bowl?]鸴?q榦軄L xiaoxianxian"簻黙
Restored: My name is? I won’t tell ordinary people!

By As can be seen in the example, before using the PHP encryption extension library Mcrypt to encrypt and decrypt data, an initialization vector, referred to as iv for short, is created as shown in $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND); The 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.

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

Encryption function $str_encrypt = mcrypt_encrypt($cipher,$key,$str,$modes,$iv); 5 of this function. The parameters are as follows: cipher - encryption algorithm, key - key, data (str) - data to be encrypted, mode - algorithm mode, iv - initialization vector

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 that data is the data that needs to be decrypted $str_encrypt, not the original Data $str.

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


http://www.bkjia.com/PHPjc/446040.html

truehttp: //www.bkjia.com/PHPjc/446040.htmlTechArticleAmong them, the Mcrypt extension library can implement encryption and decryption functions, that is, it can encrypt plain text and restore cipher text. 1. PHP encryption extension library Mcrypt installation In the standard PHP installation process, there is no...
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
How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

How can you trace session activity in PHP?How can you trace session activity in PHP?Apr 27, 2025 am 12:10 AM

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

How can you use a database to store PHP session data?How can you use a database to store PHP session data?Apr 27, 2025 am 12:02 AM

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

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

Video Face Swap

Video Face Swap

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

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function