How to encrypt and decrypt a mcrypt extension library

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

The following are the referenced contents:
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 you need to download it and download the address: After downloading, compile with the following method and expand it in PHP:

Download the MCrypt package.

The following are the referenced contents:
make Install
CD to your PHP directory.
./configure-with-mcrypt=[dir] [--other-configuration-directives]
make Install

According to your requirements and PHP installation with the server to make appropriate changes.

How do I use the MCrypt extension library to encrypt data?

First, we'll explain how to use the MCrypt extension library to encrypt data, and then explain how to use it to decrypt. The following code demonstrates this process by encrypting the data, displaying the encrypted data in the browser, and restoring the encrypted data to the original string, which is displayed on the browser.

Use MCrypt to add and decrypt data

The following are the referenced contents:
//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 are the referenced contents:
original string:applied cryptography, by Bruce Schneier, is a wonderful cryptography.

encrypted string:02a7c58b1ebd22a9523468694b091e60411cc4dea8652bb8072 34fa06bbfb20e71ecf525f29df58e28f3d9bf541f7ebcecf62b C89fde4d8e7ba1e6cc9ea24850478c11742f5cfa1d23fe22fe8 bfbab5e

decrypted string:applied cryptography, by Bruce Schneier, is a wonderful cryptography.

The two most typical functions in the above code are Mcrypt_encrypt () and Mcrypt_decrypt (), and their purpose is obvious. We use the "Telegraph cipher This" mode, MCrypt provides several encryption methods, because each encryption method has the specific characters that can affect the security of the password, therefore need to understand each pattern. For those readers who have not contacted the cryptography system, it may be more interested in the Mcrypt_create_iv () function, and we will mention the initialization vector (hence, iv) 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 supplied in the required pattern.

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: 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.