[php] Fatal error: Call to undefined function mcrypt_encrypt()

NOTE: The libraries MCrypt support depend on have not been updated in years and MCrypt should no longer be considered a viable or secure method of encrypting data. What's more, MCrypt has been deprecated in PHP 5, and removed entirely in PHP 7. If you have any code that runs MCrypt you should refactor it to use a more modern encryption library.


Does anyone know why this error message: (Call to undefined function mcrypt_encrypt() ) displays when I run the following code below?

Am I missing some steps perhaps any setting in PHP I have to do before this code can work?

$key = 'password to (en/de)crypt';
$string = 'string to be encrypted';

$test = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key),
            $string, MCRYPT_MODE_CBC, md5(md5($key)));

This question is related to php mcrypt

The answer is


On Linux Mint 17.1 Rebecca - Call to undefined function mcrypt_create_iv...

Solved by adding the following line to the php.ini

extension=mcrypt.so

After that a

service apache2 restart

solved it...


You don't have the mcrypt library installed.

See http://www.php.net/manual/en/mcrypt.setup.php for more information.

If you are on shared hosting, you can ask your provider to install it.


In OSX you can easily install mcrypt via homebrew

brew install php54-mcrypt --without-homebrew-php

Then add this line to /etc/php.ini.

extension="/usr/local/Cellar/php54-mcrypt/5.4.24/mcrypt.so"

If you have recently updated to ubuntu 14.04 here is the fix to this problem:

$ sudo mv /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available/
$ sudo php5enmod mcrypt
$ sudo service apache2 restart

If you using ubuntu 14.04 here is the fix to this problem:

First check php5-mcryp is already installed apt-get install php5-mcrypt

If installed, just run this two command or install and run this two command

$ sudo php5enmod mcrypt
$ sudo service apache2 restart

I hope it will work.


In Ubuntu 18.04, and for php7.0

$ sudo apt-get install php7.0-mcrypt

$ sudo systemctl reload apache2

For me it helped to uninstall mcrypt with:

sudo apt-get purge php5-mcrypt

and simply reinstall it:

sudo apt-get install php5-mcrypt

and dont forget to restart apache as described above.

Dont know why and how this was different in my case (using a vm with provisioned php55), but maybe this will help someone else. I also had this problem with some other modules like xcache...


If you are using PHP 7.2 or up:

This function was DEPRECATED in PHP 7.1.0, and REMOVED in PHP 7.2.0.

source: http://php.net/manual/en/function.mcrypt-encrypt.php

So you have to replace the php code and find a solution without mcrypt.

Or, I just found out, you can STILL use mcrypt in PHP 7.2.0, but you have to install it as a PHP Extension Community Library. (https://pecl.php.net/)

On Debian/Ubuntu Linux distros:

sudo apt-get -y install gcc make autoconf libc-dev pkg-config
sudo apt-get -y install php7.2-dev
sudo apt-get -y install libmcrypt-dev

then:

sudo pecl install mcrypt-1.0.1

Source: https://www.techrepublic.com/article/how-to-install-mcrypt-for-php-7-2/


Under Ubuntu I had the problem and solved it with

$ sudo apt-get install php5-mcrypt
$ sudo service apache2 reload

One more thing: if you are serving PHP via a web server such as Apache, try restarting the web server. This will "reset" any PHP modules that might be present, activating the new configuration.


Assuming you are using debian linux (I'm using Linux mint 12, problem was on Ubuntu 12.04.1 LTS server I ssh'ed into.)

I suggest taking @dkamins advice and making sure you have mcrypt installed and active on your php5 install. Use "sudo apt-get install php5-mcrypt" to install. My notes below.

Using PHP version PHP Version 5.3.10-1ubuntu3.4, if you open phpinfo() as suggested by @John Conde, which you do by creating test file on web server (e.g. create status page testphp.php with just the contents "" anywhere accessible on the server via browser)

I found no presence of enabled or disabled status on the status page when opened in browser. When I then opened the php.ini file, mentioned by @Anthony Forloney, thinking to uncomment ;extension=php_mcrypt.dll to extension=php_mcrypt.dll

I toggled that back and forth and restarted Apache (I'm running Apache2 and you can restart in my setup with sudo /etc/init.d/apache2 restart or when you are in that directory just sudo restart I believe) with change and without change but all no go. I took @dkamins advice and went to install the package with "sudo apt-get install php5-mcrypt" and then restarted apache as above. Then my error was gone and my application worked fine.


On ubuntu 14.10 :

Install module mcrypt

sudo apt install php5-mcrypt

Enable module mcrypt on apache2

sudo a2enmod mcrypt

Reload module configuration

sudo service apache2 restart

If you are using php5-fpm do remeber to restart it, after installing mcrypt

service php5-fpm restart


Is mcrypt enabled? You can use phpinfo() to see if it is.


I had the same issue for PHP 7 version of missing mcrypt.

This worked for me.

sudo apt-get update
sudo apt-get install mcrypt php7.0-mcrypt
sudo apt-get upgrade
sudo service apache2 restart (if needed)

for Linux based (Fedora)

yum -y install php-mcrypt

Enable the module by adding: 'extension=mcrypt.so' to PHP.ini. (/etc/php.ini)

systemctl restart httpd.service

Done!


For windows

;extension=php_mcrypt.dll to extension=php_mcrypt.dll 
 then restart your apache server

For Redhat

sudo yum install php55-mcrypt //if php5.5
sudo yum install php-mcrypt //if less than 5.4
sudo service httpd restart //if apache 2.4
sudo /etc/init.d/httpd restart //if apache 2.2 or less

For Ubuntu

sudo apt-get install php5-mcrypt
sudo service apache2 restart //if server not reloaded automatically 

Still not working?

sudo php5enmod mcrypt && sudo service apache2 restart


Check and install php5-mcrypt:

sudo apt-get install php5-mcrypt

My Environment: Windows 10, Xampp Control Panel v3.2.4, PHP 7.3.2

Step-1: Download a suitable version for your system from here: https://pecl.php.net/package/mcrypt/1.0.3/windows

Step-2: Unzip and copy php_mcrypt.dll file to ../xampp/php/ext/

Step-3: Open ../xampp/php/php.ini file and add a line extension=php_mcrypt.dll

Step-4: Restart apache, DONE!