How to use the MCrypt extension library for encryption and decryption _php tutorial

Source: Internet
Author: User
Tags mcrypt
Summary: Mcrypt 2.4.7 is a powerful cryptographic algorithm extension library that includes 22 algorithms, including the following:

The following is the referenced content:

Blowfish RC2 safer-sk64 Xtea

Cast-256 RC4 safer-sk128

DES Rc4-iv Serpent

Enigma Rijndael-128 Threeway

Gost Rijndael-192 TripleDES

LOKI97 Rijndael-256 Twofish

Panamasaferplus Wake

How do I install MCrypt?

MCrypt is not included in the standard PHP package, so it needs to be downloaded and the download address is: ftp://argeas.cs-net.gr/pub/unix/mcrypt/. After downloading, follow the instructions below to compile and expand it in PHP:

Download the MCrypt package.

The following is the referenced content:

Gunzipmcrypt-x.x.x.tar.gz

Tar-xvfmcrypt-x.x.x.tar

./configure--disable-posix-threads

Make

Make install

CD to your PHP directory.

./configure-with-mcrypt=[dir] [--other-configuration-directives]

Make

Make install

Make the appropriate modifications to your request and to the server when installing PHP.

How do I encrypt data using the MCrypt extension library?

First, we'll show you how to encrypt the data using the MCrypt extension library, and then explain how to use it for decryption. The following code demonstrates this process by encrypting the data, then displaying the encrypted data on the browser and restoring the encrypted data to the original string and displaying it on the browser.

Use MCrypt to add and decrypt data

The following is the referenced content:

Designate string to be encrypted

$string = "Applied Cryptography, by Bruce Schneier, is

A wonderful cryptography reference. ";

Encryption/decryption Key

$key = "Four score and twenty years ago";

Encryption algorithm

$cipher _alg = mcrypt_rijndael_128;

Create the initialization vector for added security.

$iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher _alg,

MCRYPT_MODE_ECB), Mcrypt_rand);

Output Original String

Print "Original string: $string

";

Encrypt $string

$encrypted _string = Mcrypt_encrypt ($cipher _alg, $key,

$string, MCRYPT_MODE_CBC, $IV);

Convert to hexadecimal and output to browser

Print "Encrypted string:". Bin2Hex ($encrypted _string). "

";

$decrypted _string = Mcrypt_decrypt ($cipher _alg, $key,

$encrypted _string, MCRYPT_MODE_CBC, $IV);

Print "decrypted string: $decrypted _string";

?>

Executing the above script will produce the following output:

The following is the referenced content:

Original string:applied cryptography, by Bruce Schneier, is a wonderful cryptography reference.

Encrypted string:02a7c58b1ebd22a9523468694b091e60411cc4dea8652bb8072 34fa06bbfb20e71ecf525f29df58e28f3d9bf541f7ebcecf62b C89fde4d8e7ba1e6cc9ea24850478c11742f5cfa1d23fe22fe8 bfbab5e

Decrypted string:applied cryptography, by Bruce Schneier, is a wonderful cryptography reference.

The two most typical functions in the above code are Mcrypt_encrypt () and Mcrypt_decrypt (), whose purpose is obvious. We use the "Telegraph cipher" mode, MCrypt provides several encryption methods, because each encryption method can affect the password security of certain characters, so for each mode need to understand. For those readers who have not contacted the password system, it may be more interesting to the Mcrypt_create_iv () function, and we will refer to the initialization vector (hence, iv) that it creates, which always allows each piece of information to be independent of each other. Although not all patterns require this initialization variable, PHP gives a warning message if the variable is not provided in the required pattern.

http://www.bkjia.com/PHPjc/814350.html www.bkjia.com true http://www.bkjia.com/PHPjc/814350.html techarticle Summary: Mcrypt 2.4.7 is a powerful cryptographic algorithm extension library that includes 22 algorithms, including the following: The following are the references: Blowfish RC2 Safe ...

  • Related Article

    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.