Encrypt and decrypt the Mcrypt Extension Library

Source: Internet
Author: User
Tags mcrypt
The limcrypt extension library is used for encryption and decryption. Read the limcrypt extension library for encryption and decryption. Summary: Mcrypt2.4.7 is a powerful encryption algorithm Extension Library, which includes 22 algorithms, which includes the following algorithms: BlowfishRC2Safer-sk64xteaCast-256RC4Safer-sk128DESRC4-ivSerp

Summary: Mcrypt 2.4.7 is a powerful Extension Library for encryption algorithms. It includes 22 algorithms, including the following algorithms:

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 to install Mcrypt?

Mcrypt is not included in the standard PHP package, so you need to download it at: ftp://argeas.cs-net.gr/pub/unix/mcrypt. After downloading the SDK, compile it as follows and expand it to PHP:

Download the Mcrypt package.

Tar -xvfmcrypt-x.x.x.tar
./Configure -- disable-posix-threads
Make install
Cd to your PHP directory.
./Configure-with-mcrypt = [dir] [-- other-configuration-directives]
Make install

Make appropriate modifications to the server during PHP installation according to your requirements.

How to use the Mcrypt Extension Library to encrypt data?

First, we will introduce how to use the Mcrypt Extension Library to encrypt data, and then how to use it for decryption. The following code demonstrates this process. first, encrypt the data, display the encrypted data in the browser, and restore the encrypted data to the original string, display it in a browser.

Use Mcrypt to encrypt and decrypt data

<? Php
// 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,

// 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 ";

Execute the above script to generate the following output:

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

Encrypted string: 201734fa06bbfb20e71ecf525f29df58e28f3d9bf541f7ebcecf62b 108bfbab5e

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 (). Their usage is obvious. We use the "Telegraph cipher book" mode. Mcrypt provides several encryption methods. because each encryption method has specific characters that can affect the password security, we need to understand each mode. For those who have never touched the password system, they may be more interested in the mcrypt_create_iv () function. we will mention the initialization vector (hence, iv) it creates ), this always makes each piece of information independent from each other. Although not all modes require this initialization variable, PHP will give a warning if this variable is not provided in the required mode.

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.