68

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)));
GordonM
  • 29,211
  • 15
  • 77
  • 126
Jin Yong
  • 38,582
  • 71
  • 132
  • 177
  • What version of PHP are you running? – Anthony Forloney Apr 09 '10 at 00:51
  • Maybe you have PHP 4.0.1, this is a 4.0.2 function. Runs properly in here. – Ben Apr 09 '10 at 00:54
  • 5
    I have version 5.2.8 and I get the undefined function error as well. – Anthony Forloney Apr 09 '10 at 00:57
  • 1
    It is best not to use mcrypt, it is abandonware, has not been updated in years and does not support standard PKCS#7 (née PKCS#5) padding, only non-standard null padding that can't even be used with binary data. mcrypt has many outstanding [bugs](https://sourceforge.net/p/mcrypt/bugs/) dating back to 2003. Instead consider using [defuse](https://github.com/defuse/php-encryption) or [RNCryptor](https://github.com/RNCryptor), they provide a complete solution and are being maintained and is correct. – zaph Mar 23 '17 at 12:57

19 Answers19

89

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
flor
  • 991
  • 1
  • 6
  • 6
  • 3
    `mcrypt.ini` was already in `/etc/php5/mods-available/` on my installation so I only had to run php5enmod. – nullability Sep 27 '14 at 16:02
  • I had to do the file copying. Thanks a lot ! – wtf8_decode Jan 15 '15 at 18:44
  • 3
    I started up a new machine and didn't have mcrypt installed -- don't forget to make sure mcrypt is installed before this step with `$ sudo apt-get install php5-mcrypt` and then check http://askubuntu.com/questions/460837/mcrypt-extension-is-missing-in-14-04-server-for-mysql – dminear Apr 01 '15 at 14:56
  • thank you!!! I had the same issue after transferring hosting to a new dev server... This is why stack overflow is great! – asherrard Apr 07 '15 at 16:50
  • I'm using vagrant ssh on a WAMPServer setup, and had to first install php5-mcrypt and then did the "sudo php5enmod mcrypt" and restarting vagrant fixed it for me thanks. – Andrew Jul 09 '15 at 13:14
  • On Ubuntu, you are NOT supposed to MOVE the file. You either create a symlink in **mods-enabled** yourself or use the **a2enmod** command to enable the mod. – inquam Feb 29 '16 at 11:15
  • This saved my life during a live demo. – Captain Hypertext Aug 03 '16 at 16:55
36

What had worked for me with PHP version 5.2.8, was to open up php.ini and allow the php_mcrypt.dll extension by removing the ;, i.e. changing:

;extension=php_mcrypt.dll to extension=php_mcrypt.dll

James Harrington
  • 2,894
  • 26
  • 29
Anthony Forloney
  • 84,001
  • 14
  • 111
  • 112
31

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

Sabbir
  • 1,312
  • 1
  • 14
  • 22
20

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/

Martin Krung
  • 1,013
  • 7
  • 22
19

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"
davidcondrey
  • 29,530
  • 14
  • 105
  • 129
dkamins
  • 20,091
  • 6
  • 48
  • 57
  • Thanks, For me path was slightly different though: extension="/usr/local/Cellar/php55-mcrypt/5.5.22/mcrypt.so" – anshuman Mar 17 '15 at 14:09
  • For me, I didn't had to edit the php.ini, it was already set. And `phpXX`, XX is for the version you're using. (`php -v` in the terminal to find out.) – Zachary Dahan May 25 '15 at 05:12
  • I don't even have the path `/usr/local/Cellar/`. Are you sure this is correct? – dspacejs Sep 16 '15 at 13:32
11

Under Ubuntu I had the problem and solved it with

$ sudo apt-get install php5-mcrypt
$ sudo service apache2 reload
Pierre de LESPINAY
  • 40,390
  • 50
  • 195
  • 282
7

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
alphayax
  • 2,354
  • 1
  • 20
  • 23
  • 3
    I think you mean: `sudo php5enmod mcrypt` – Pere Barceló Oct 28 '15 at 11:05
  • `php5enmod` and `php5dismod` are new commands introduced on debian systems for php (>= 5.4.0~rc6-1). According to this post http://php.net/manual/fr/mcrypt.setup.php#116231 you just have to do sudo `apt-get install php5-mcrypt` `sudo php5enmod mcrypt` `sudo service apache2 reload` – alphayax Nov 11 '15 at 11:27
6

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...

Phil Lawlor
  • 173
  • 2
  • 4
5

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)
  • This works! But only needed php7.0-mcrypt. After installed it, and restarted apache, it is now showing mcrypt on phpinfo(); `Registered Stream Filters zlib.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, convert.iconv.*, mcrypt.*, mdecrypt.*` – Hudson Santos Apr 19 '17 at 12:28
3

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.

Stephan
  • 37,597
  • 55
  • 216
  • 310
beiping_troop
  • 147
  • 1
  • 2
  • 5
3

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

John Conde
  • 207,509
  • 96
  • 428
  • 469
3

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

service php5-fpm restart

nicky
  • 3,581
  • 7
  • 33
  • 41
3

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.

Rajesh Jai
  • 71
  • 2
3

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.

Mei
  • 1,059
  • 1
  • 11
  • 19
2

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!

Reza Mamun
  • 5,137
  • 36
  • 36
1

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!

Somwang Souksavatd
  • 3,974
  • 26
  • 28
1

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...

Heisoka
  • 68
  • 6
1

In Ubuntu 18.04, and for php7.0

$ sudo apt-get install php7.0-mcrypt

$ sudo systemctl reload apache2
ShivarajRH
  • 806
  • 10
  • 23
0

Check and install php5-mcrypt:

sudo apt-get install php5-mcrypt
Fabricio
  • 2,977
  • 2
  • 12
  • 20