How to use the MCrypt extension library for encryption and decryption

Source: Internet
Author: User
Tags decrypt mcrypt

Summary: 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: ftp://argeas.cs-net.gr/pub/unix/mcrypt/. After downloading, compile with the following method and expand it in PHP:

Download the MCrypt package.

The following are the referenced contents:

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

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