Summary of MCrypt Use of PHP security processing

Source: Internet
Author: User

http://blog.csdn.net/why_2012_gogo/article/details/51194673

PHP MCrypt Use summary of security processing:

We know that when writing code programs, in addition to ensuring the high performance of the code, there is a very important thing is the security of the data. For PHP, it itself provides several ways to encrypt data, but still limited, to meet the special data encryption and decryption of some shortcomings, so the recommended third-party expansion MCrypt Library, which provides a variety of types, algorithms and modes of encryption and decryption function, then the following to describe its use.

· Installation and Configuration

· Pre-defined constants

· Validation of examples

First, installation and configuration

1. Installation

Because MCrypt is used as an extension by PHP, you can refer to the PHP extension article:

http://blog.csdn.net/why_2012_gogo/article/details/51120645

2. Configuration

For the MCrypt configuration is simple, we just need to add the following in the php.ini:

Extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/mcrypt.so

In fact, that is, add extension load just.

Second, predefined constants

Data encryption mode supported by MCrypt:

MCRYPT_MODE_ECB (electronic codebook): Suitable for random data, such as the ability to encrypt other keys in this mode. Because the data to be encrypted is very short and random, the disadvantage of this model has a positive effect.

MCRYPT_MODE_CBC (cipher Block chaining): Ideal for encrypting files. Its security has improved significantly compared to the ECB.

MCRYPT_MODE_CFB (cipher feedback) is encrypted for each individual byte, so it is well suited for encryption for byte streams.

MCRYPT_MODE_OFB (output feedback, in 8bit): Similar to CFB . It can be used in applications that cannot tolerate the propagation of cryptographic errors. Because it is encrypted according to 8 bits, the safety factor is low and is not recommended.

libmcrypt-2.4.x or later:

MCRYPT_MODE_NOFB (output feedback, in Nbit): Similar to OFB, but more secure because it can encrypt data according to the packet size specified by the algorithm.

Mcrypt_mode_stream is an extended pattern that contains a stream encryption algorithm such as "WAKE" or "RC4" .

Iii. Validation of examples

Here's an example to illustrate its use:

<?php

256-bit key

$key = hash (' sha256 ', ' Thisis a secret key ', true);

Open Algorithms and patterns

$TD = Mcrypt_module_open (' rijndael-256 ', ' ', ' CBC ', ');

Create an initial vector

$iv =mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), mcrypt_dev_urandom);

echo ' before encryption: This is a testting for mcryptuse!</br> ';

Initializing encryption

Mcrypt_generic_init ($TD, $key, $IV);

Encrypt string

$encrypt _data =mcrypt_generic ($TD, ' This was a testting for MCrypt use! ');

Output encrypted content

echo ' after encryption: '. $encrypt _data. ' </br> ';

End encryption, perform cleanup work

Mcrypt_generic_deinit ($TD);

Initial Dissolve secret

Mcrypt_generic_init ($TD, $key, $IV);

Decryption text

$dencrypt _data =mdecrypt_generic ($TD, $encrypt _data);

Output decrypted content

echo ' decryption: '. $dencrypt _data. ' </br> ';

End decryption, perform cleanup work

Mcrypt_generic_deinit ($TD);

Turn off the cryptographic module

Mcrypt_module_close ($TD);

?>

Results:

Well, here we have already introduced the use of MCrypt.

Summary of MCrypt Use of PHP security processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.