14.8. Encrypting and Decrypting Data
$key = 'That golden key that opes the palace of eternity.'; $data = 'The chicken escapes at dawn. Send help with Mr. Blue.'; $alg = MCRYPT_BLOWFISH; $mode = MCRYPT_MODE_CBC; $iv = mcrypt_create_iv(mcrypt_get_iv_size($alg,$mode),MCRYPT_DEV_URANDOM); $encrypted_data = mcrypt_encrypt($alg, $key, $data, $mode, $iv); $plain_text = base64_encode($encrypted_data); print $plain_text."\n"; $decoded = mcrypt_decrypt($alg,$key,base64_decode($plain_text),$mode,$iv); print $decoded."\n"; NNB9WnuCYjyd3Y7vUh7XDfWFCWnQY0BsMehHNmBHbGOdJ3cM+yghABb/XyrJ+w3xz9tms74/a70= The chicken escapes at dawn. Send help with Mr. Blue.
The mcrypt extension is an interface with mcrypt, a library that implements many different encryption algorithms. The data is encrypted and decrypted by mcrypt_encrypt( ) and mcrypt_decrypt( ), respectively. They each take five arguments. The first is the algorithm to use. To find which algorithms mcrypt supports on your system, call mcrypt_list_algorithms( ). The full list of mcrypt algorithms is shown in Table 14-1. The second argument is the encryption key; the third argument is the data to encrypt or decrypt. The fourth argument is the mode for the encryption or decryption (a list of supported modes is returned by mcrypt_list_modes( )). The fifth argument is an initialization vector (IV), used by some modes as part of the encryption or decryption process.
Table 14-1 lists all the possible mcrypt algorithms, including the constant value used to indicate the algorithm, the key and block sizes in bits, and whether the algorithm is supported by libmcrypt 2.2.x and 2.4.x.
Table 14-1. mcrypt algorithm constants
Except for the data to encrypt or decrypt, all the other arguments must be the same when encrypting and decrypting. If you're using a mode that requires an initialization vector, it's okay to pass the initialization vector in the clear with the encrypted text.
The different modes are appropriate in different circumstances. Cipher Block Chaining (CBC) mode encrypts the data in blocks, and uses the encrypted value of each block (as well as the key) to compute the encrypted value of the next block. The initialization vector affects the encrypted value of the first block. Cipher Feedback (CFB) and Output Feedback (OFB) also use an initialization vector, but they encrypt data in units smaller than the block size. Note that OFB mode has security problems if you encrypt data in smaller units than its block size. Electronic Code Book (ECB) mode encrypts data in discreet blocks that don't depend on each other. ECB mode doesn't use an initialization vector. It is also less secure than other modes for repeated use, because the same plaintext with a given key always produces the same ciphertext. Constants to set each mode are listed in Table 14-2.
Table 14-2. mcrypt mode constants
Different algorithms have different block sizes. You can retrieve the block size for a particular algorithm with mcrypt_get_block_size( ) . Similarly, the initialization vector size is determined by the algorithm and the mode. mcrypt_create_iv( ) and mcrypt_get_iv_size( ) make it easy to create an appropriate random initialization vector:
$iv = mcrypt_create_iv(mcrypt_get_iv_size($alg,$mode),MCRYPT_DEV_URANDOM);
The first argument to mcrypt_create_iv( ) is the size of the vector, and the second is a source of randomness. You have three choices for the source of randomness. MCRYPT_DEV_RANDOM reads from the pseudodevice /dev/random, MCRYPT_DEV_URANDOM reads from the pseudo-device /dev/urandom, and MCRYPT_RAND uses an internal random number generator. Not all operating systems support random-generating pseudo-devices. Make sure to call srand( ) before using MCRYPT_RAND in order to get a nonrepeating random number stream.
The code and examples in this recipe are compatible with mcrypt 2.4. PHP's mcrypt interface supports both mcrypt 2.2 and mcrypt 2.4, but there are differences between the two. With mcrypt 2.2, PHP supports only the following mcrypt functions: mcrypt_ecb( ), mcrypt_cbc( ), mcrypt_cfb( ), mcrypt_ofb( ), mcrypt_get_key_size( ), mcrypt_get_block_size( ), mcrypt_get_cipher_name( ), and mcrypt_create_iv( ). To encrypt or decrypt data with mcrypt 2.2, call the appropriate mcrypt_MODE( ) function, based on what mode you want to use, and pass it an argument that instructs it to encrypt or decrypt. The following code is the mcrypt 2.2-compatible version of the code in the Solution:
$key = 'That golden key that opes the palace of eternity.'; $data = 'The chicken escapes at dawn. Send help with Mr. Blue.'; $alg = MCRYPT_BLOWFISH; $iv = mcrypt_create_iv(mcrypt_get_block_size($alg),MCRYPT_DEV_URANDOM); $encrypted_data = mcrypt_cbc($alg,$key,$data,MCRYPT_ENCRYPT); $plain_text = base64_encode($encrypted_data); print $plain_text."\n"; $decoded = mcrypt_cbc($alg,$key,base64_decode($plain_text),MCRYPT_DECRYPT); print $decoded."\n";
14.8.4. See Also
Documentation on the mcrypt extension at http://www.php.net/mcrypt; the mcrypt library is available at http://mcrypt.hellug.gr/; choosing an appropriate algorithm and using it securely requires care and planning: for more information about mcrypt and the cipher algorithms it uses, see the online PHP manual section on mcrypt, the mcrypt home page, and the manpages for /dev/random and /dev/urandom; good books about cryptography include Applied Cryptography, by Bruce Schneier (Wiley) and Cryptography: Theory and Practice, by Douglas R. Stinson (Chapman & Hall).
Copyright © 2003 O'Reilly & Associates. All rights reserved.