Error: Call to undefined function mcrypt_encrypt()
Description: In one of our project, I was trying to save a field value in encrypted format in the database, using PHP, which could be retrieved back to its normal value, where we need to display it.
So for that, I was trying to encrypt the value using PHP method mcrypt_encrypt() as follows:
$key = ‘some unique key value’;
$key contains the value which we are going to encrypt/decrypt.
$encryptedValue = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $normalValue, MCRYPT_MODE_CBC, md5(md5($key))));
$normalValue = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encryptedValue), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
But after using this, I was getting a fatal error. When I checked in debug mode, I came to know that it was happening due to the lack of mcrypt libraries on my local machine and I needed to install the mcrypt libraries for php5, on my ubuntu machine.
For that, I used following three commands in the terminal, for php5:
1. apt-get install php5-mcrypt
2. php5enmod mcrypt
3. service apache2 restart
Note: If you are using php7, then you can use the following command:
apt install php7.0-mcrypt (instead of apt-get install php5-mcrypt)
And after that, I was successfully able to use the above PHP method on my machine.