Home > Article > Backend Development > How to use the Mcrypt extension library for encryption and decryption_PHP tutorial
Overview: Mcrypt 2.4.7 is a powerful encryption algorithm extension library, which includes 22 algorithms, including the following algorithms:
The following is the quoted 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 to install Mcrypt?
Mcrypt is not included in the standard PHP software package, so you need to download it. The download address is: ftp://argeas.cs-net.gr/pub/unix/mcrypt/. After downloading, compile it as follows and expand it in PHP:
Download the Mcrypt software package.
The following is the quoted 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 appropriate modifications according to your requirements and the server during PHP installation.
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 introduce how to use it to decrypt. The following code demonstrates this process. It first encrypts the data, then displays the encrypted data on the browser, restores the encrypted data to the original string, and displays it on the browser.
Use Mcrypt to encrypt and decrypt data
The following is the quoted 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 quoted content:
Original string: Applied Cryptography, by Bruce Schneier, is a wonderful cryptography reference.
Encrypted string: 02a7c58b1ebd22a9523468694b091e60411cc4dea8652bb8072 34fa06bbfb20e71ecf525f29df58e28f3d9bf541f7ebcecf62b c89fde4d8e7ba1e6cc9 ea24850478c11742f5cfa1d23fe22fe8 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(), and their uses are obvious. We used the "Telegraph Codebook" mode. Mcrypt provides several encryption methods. Since each encryption method has specific characters that can affect the security of the password, you need to understand each mode. For those readers who have not been exposed to cryptography systems, they may be more interested in the mcrypt_create_iv() function. We will mention the initialization vector (hence, iv) it creates, which can make each piece of information independent of each other. Although not all modes require this initialization variable, PHP will give a warning message if this variable is not provided in the required mode.