Others than securing data using encoding such as base64, if you are good in ciphers, you may utilise the mcrypt encryptions instead for better data encryptions.
Mcrypt is the utilities where you can use it to encrypt the data with your own desirable ciphers for encryptions.
The most common ciphers used will be Rijndael, TwoFish, DES, TripleDES (3DES). To see what other available ciphers, you may refer to the follow url links:
http://my.php.net/manual/en/mcrypt.ciphers.php
Before start using mcrypt, please make sure that you have enable it for PHP. PHP by default is having this component/module disabled. For Linux, compile or recompile your PHP engine with “–with-mcrypt”. For Windows, enable it via php.ini by uncomment the line which contains “extension=php_mcrypt.dll”.
If you are compiling PHP as Apache module, please make sure that you restarted the apache service once the PHP finishes compiling with mcrypt.
Using mycrypt is rather simple if you have already understand how each function works and how the function call sequence.
The basic function which you will use for data encryption/decryption via mcrypt:
mcrypt_get_iv_size() function
mcrypt_get_iv_size ( string $cipher , string $mode )
mcrypt_get_iv_size() returns the size of the Initialisation Vector (IV) in bytes. On error the function returns FALSE. If the IV is ignored in the specified cipher/mode combination zero is returned.
cipher is one of the MCRYPT_ciphername constants of the name of the algorithm as string.
mode is one of the MCRYPT_MODE_modename constants or one of “ecb”, “cbc”, “cfb”, “ofb”, “nofb” or “stream”. The IV is ignored in ECB mode as this mode does not require it. You will need to have the same IV (think: starting point) both at encryption and decryption stages, otherwise your encryption will fail.
mcrypt_create_iv() function
mcrypt_create_iv ( int $size [, int $source ] )
mcrypt_create_iv() is used to create an IV.
Parameter size determines the size of the IV, parameter source (defaults to random value) specifies the source of the IV.
The source can be MCRYPT_RAND (system random number generator), MCRYPT_DEV_RANDOM (read data from /dev/random) and MCRYPT_DEV_URANDOM (read data from /dev/urandom). MCRYPT_RAND is the only one supported on Windows because Windows (of course) doesn’t have /dev/random or /dev/urandom.
mcrypt_encrypt() function
mcrypt_encrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] )
mcrypt_encrypt() encrypts the data and returns the encrypted data.
Cipher is one of the MCRYPT_ciphername constants of the name of the algorithm as string.
Key is the key with which the data will be encrypted. If it’s smaller that the required keysize, it is padded with ‘\0′. It is better not to use ASCII strings for keys. It is recommended to use the mhash functions to create a key from a string.
Data is the data that will be encrypted with the given cipher and mode. If the size of the data is not n * blocksize, the data will be padded with ‘\0′. The returned crypttext can be larger that the size of the data that is given by data .
Mode is one of the MCRYPT_MODE_modename constants of one of “ecb”, “cbc”, “cfb”, “ofb”, “nofb” or “stream”.
The IV parameter is used for the initialisation in CBC, CFB, OFB modes, and in some algorithms in STREAM mode. If you do not supply an IV, while it is needed for an algorithm, the function issues a warning and uses an IV with all bytes set to ‘\0′.
mcrypt_decrypt() function
mcrypt_decrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] )
cipher is one of the MCRYPT_ciphername constants of the name of the algorithm as string.
key is the key with which the data is encrypted. If it’s smaller that the required keysize, it is padded with ‘\0′.
data is the data that will be decrypted with the given cipher and mode. If the size of the data is not n * blocksize, the data will be padded with ‘\0′.
mode is one of the MCRYPT_MODE_modename constants of one of “ecb”, “cbc”, “cfb”, “ofb”, “nofb” or “stream”.
The iv parameter is used for the initialisation in CBC, CFB, OFB modes, and in some algorithms in STREAM mode. If you do not supply an IV, while it is needed for an algorithm, the function issues a warning and uses an IV with all bytes set to ‘\0′.
The sequence of the function call for encrypting a data will be as below:
mcrypt_get_iv_size() >> mcrypt_create_iv() >> mcrypt_encrypt()
The sequence of the function call for decrypting an encrypted string will be as below:
mcrypt_get_iv_size() >> mcrypt_create_iv() >> mcrypt_decrypt()
The sample encryption and decryption are shown below:
function encrypt_data($keystr,$datastring)
{
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256,MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $keystr, $datastring, MCRYPT_MODE_ECB, $iv);
return $crypttext;
}
function decrypt_data($keystr,$encryptedstr)
{
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256,MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $keystr, $encryptedstr, MCRYPT_MODE_ECB, $iv);
return $decrypttext;
}
Notes:
Programmers hate internet browsers. Why? This is due to the more variety the internet browser is, the more consideration they need to make when doing the codes to make sure that the codes are compatible with majorities of the browsers.
For MCRYPT, there is compatibility issue found with Firefox where you will see question mark “?” symbol appear when trying to display out the decrypted message.
To fix this, contact Mozilla to check on their browsers or boycott em’ :p. Just kidding.. to fix it, just simply use the trim() function to trim the decrypted message before display the value.
You may click here to download the sample scripts for data encryption using mcrypt.