14

I've asked the system admins to install mcrypt on the server, and they say everything is OK. But when I run my simple script I get this.

Warning: mcrypt_get_iv_size() [function.mcrypt-get-iv-size]: Module initialization failed

It's coming from this line:

$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256,MCRYPT_MODE_ECB);

Now, from this code:

$algorithms = mcrypt_list_algorithms("/usr/local/bin/mcrypt");

foreach ($algorithms as $cipher) {
    echo "$cipher<br />\n";
} 

I get:

Warning: mcrypt_list_algorithms() [function.mcrypt-list-algorithms]: No algorithms found in module dir

When I run this:

$modes = mcrypt_list_modes("/usr/local/bin/mcrypt");

foreach ($modes as $mode) {
    echo "$mode <br />\n";
}

I get:

Warning: mcrypt_list_modes() [function.mcrypt-list-modes]: No modes found in module dir

However, running this command this command

which mcrypt; mcrypt -v

Produces:

/usr/local/bin/mcrypt
Mcrypt v.0.9.9 (x86_64-unknown-linux-gnu)
Linked against libmcrypt v.2.5.7
Copyright (C) 1998-2002 Nikos Mavroyanopoulos (nmav@gnutls.org)

Is mcrypt installed/configured correctly?

UPDATE:

I did this:

whereis libmcrypt

...and got the result:

libmcrypt: /usr/local/lib/libmcrypt /usr/local/lib/libmcrypt.so /usr/local/lib/libmcrypt.la

I've also changed:

$modes = mcrypt_list_modes("/usr/local/bin/mcrypt");

to...

$modes = mcrypt_list_modes("/usr/local/lib/libmcrypt");
$algorithms = mcrypt_list_algorithms("/usr/local/lib/libmcrypt");

As recommended, but still get the error:

Warning: mcrypt_list_modes() [function.mcrypt-list-modes]: No modes found in module dir in

Then I went into the file manager in my CPanel to /usr/local/lib/libmcrypt. The directory exists but it has no files in it...

Here is the print screen of the file manager.

Thanks.

netcoder
  • 66,435
  • 19
  • 125
  • 142
Kide
  • 141
  • 1
  • 1
  • 4
  • cypher and mode are both constants and NOT a string, if you treat it as a string, then you receive the warning, try to call it just like a constant and will work. For ex. echo "$cipher
    \n"; is not the same of echo MCRYPT_TWOFISH; The same for the constant mode -> echo MCRYPT_MODE_ECB; is not the same as echo "$mode";
    – devasia2112 Feb 03 '14 at 13:37

6 Answers6

35

Try this for your mcrypt problem:

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

It's a bug with the location of the mcrypt.ini file.

I had the same bug, I did a cp instead of a mv to be sure but it solved the problem.

For PHP not working, if you get phpmyadmin working (even with the mcrypt error), it means PHP is working (because phpmyadmin uses PHP). But in your example

<? echo $row['details']; ?>
change <? to <?php and try again?
Akarsh Satija
  • 1,756
  • 2
  • 22
  • 28
  • 1
    This was exactly my problem. Looks like a bug to me. This should be done automatically when installing php5-mcrypt or similar. – Mike Mar 12 '14 at 03:29
5

On Ubuntu 14.04 just need to do only three steps as sudo:

apt-get install php5-mcrypt
php5enmod mcrypt
service apache2 restart
chopper
  • 6,649
  • 7
  • 36
  • 53
Ernest
  • 2,039
  • 24
  • 19
0

The mcrypt algorithms directory must be a directory containing the algorithms, not a binary. This means that this:

$modes = mcrypt_list_modes("/usr/local/bin/mcrypt");

Should be this instead (assuming mcrypt is installed is /usr/local/lib/libmcrypt):

$modes = mcrypt_list_modes("/usr/local/lib/libmcrypt");

If the directory where libmcrypt is installed is not that one above, you have to update your php.ini setting. Taken from php.ini:

[mcrypt]

; Directory where to load mcrypt algorithms
; Default: Compiled in into libmcrypt (usually /usr/local/lib/libmcrypt)
;mcrypt.algorithms_dir=

; Directory where to load mcrypt modes
; Default: Compiled in into libmcrypt (usually /usr/local/lib/libmcrypt)
;mcrypt.modes_dir=

If you have shell access, you can try this command to find the lib directory for your mcrypt installation:

whereis libmcrypt

or...

whereis mcrypt

Depending on your setup.

netcoder
  • 66,435
  • 19
  • 125
  • 142
  • netcoder I posted the reply as an answer because that was the only way I could have the styling ... and the message was too long for reply field? I do apologize because of that. – Kide Jan 26 '11 at 22:25
  • @user591267: What does `mcrypt --list-hash` returns in command line? – netcoder Jan 26 '11 at 23:35
  • It returns: Supported Hash Algorithms: crc32 md5 sha1 haval256 ripemd160 tiger gost crc32b haval224 haval192 haval160 haval128 tiger128 tiger160 md4 sha256 adler32 sha224 sha512 sha384 whirlpool ripemd128 ripemd256 ripemd320 snefru128 snefru256 md2 – Kide Jan 27 '11 at 13:46
0

I had exactly the same problem.

> mcrypt -v
Mcrypt v.0.9.9 (sparc-sun-solaris2.10)
Linked against libmcrypt v.2.5.8

> mcrypt --list-hash
Supported Hash Algorithms:
crc32
md5
sha1
haval256
ripemd160
tiger
gost
[...]

> php CryptoTest.php
PHP Warning:  mcrypt_list_modes(): No modes found in module dir in [...]

It was solved by reconfiguring and recompiling libmcrypt with these options:

./configure --prefix=$YOUR_PATH/libmcrypt --enable-dynamic-loading --disable-posix-threads

Then in your php.ini :

mcrypt.algorithms_dir=<YOUR_PATH>/libmcrypt/lib/libmcrypt
mcrypt.modes_dir=<YOUR_PATH>/libmcrypt/lib/libmcrypt
Charles
  • 9
  • 2
-1

How to install mcrypt on MAC OSX:

Hello, tested and working on Mac OSX El Capitan:

1. Download mcrypt: http://www.viames.it/download/elcapitan/mcrypt.so

2. Place it in:

/usr/lib/php/extensions/mcrypt

3. Modify your 'php.ini'

/etc/php.ini

and add the following line:

extension = /usr/lib/php/extensions/mcrypt.so

4. Check if 'mcrypt' is loaded with the following command:

php -m

php -m console result

5. Create a 'phpinfo.php' with content:

<?php echo phpinfo(); ?>

and see if mcrypt is listed:

phpinfo() shows now mcrypt to be loaded

Yes, I know I'm a gangsta.

Placeholder
  • 4,651
  • 6
  • 33
  • 35
  • 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 had 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 Sep 27 '16 at 18:43
  • Yeah, agree. However the reason I wrote this post was because I had to deal with a legacy project which required 'mcrypt' so basically I wanted to save the trouble of anyone else ending up in that specific case. – Placeholder Sep 28 '16 at 06:19
-1

Architecture: Centos 6 / Apache 2.2 / Dual PHP installation (5.3 and 5.4)

UPDATE: This solution explains how to install the module mcrypt with PHP 5.4 manually from Remi's RPM on Centos 6, but this RPM requires glibc-2.14+, so this only is not enough to make it work, if your glibc is older than that.

In my configuration, Yum install php-mcrypt* showed as installed the version for 5.3 in normal root:

php-mcrypt.x86_64                    5.3.3-5.el6                                  @epel

However, 5.4 is chrooted in /opt/rh/php54

Simply copying the 5.3 modules and config files does not work. The module fails initialization, that's why it does not appear in phpinfo(), this can be checked with

/opt/rh/php54/root/usr/bin/php  -v

The solution that worked for me is to download the Remi's RPM (although it's normally easier to install it by adding his Yum repository as discussed in the Comments) (https://www.rpmfind.net/linux/RPM/remi/fedora/25/x86_64/php54-php-mcrypt-5.4.45-12.fc25.remi.x86_64.html)

wget ftp://fr2.rpmfind.net/linux/remi/fedora/25/remi/x86_64/php54-php-mcrypt-5.4.45-12.fc25.remi.x86_64.rpm

extract the files with RPM2CPIO:

rpm2cpio php54-php-mcrypt-5.4.45-12.fc25.remi.x86_64.rpm |cpio -idmv
./opt/remi/php54/root/etc/php.d/mcrypt.ini
./opt/remi/php54/root/usr/lib64/php/modules/mcrypt.so
98 blocks

And copy to the chroot folder

cp ./opt/remi/php54/root/etc/php.d/mcrypt.ini /opt/rh/php54/root/etc/php.d/mcrypt.ini
cp ./opt/remi/php54/root/usr/lib64/php/modules/mcrypt.so /opt/rh/php54/root/usr/lib64/php/modules/

To check:

/opt/rh/php54/root/usr/bin/php  -v

And also:

<html>
<body>
<?php
phpinfo();
?>
</body>
</html>
  • If you use php54 from RHSCL (or centos-scl) the simple way is to use the php54more repository, see: https://www.softwarecollections.org/en/scls/remi/php54more/. BTW PHP 5.4 is EOL, and mcrypt is a huge trap, see: https://blog.remirepo.net/post/2015/07/07/A-propos-de-libmcrypt-et-php-mcrypt – Remi Collet Oct 30 '16 at 10:22
  • Thanks for the comments, unfortunately, mcrypt is a library required by RedSys, which is the leading online payments gateway in Spain, fomerly Sermepa, and used by customers of the leading banks (Santander, BBVA, Caixa) so it is not my choice. With regards to not to use the php54more repository, it is because of security. For business purposes, it's difficult to justify to use among the binary sources, a privately maintained repo (anyway cheers for that awesome service to the community!) so I wanted to know when I go that way. – Jose Manuel Gomez Alvarez Oct 30 '16 at 23:38
  • Both "php54more" and "remi" repositories are community repo, and same maintainer ;) – Remi Collet Oct 31 '16 at 14:10