Function mcrypt_get_iv_size() is deprecated on Laravel 5.5 and php 7.1.11

Viewed 4856

I have upgraded laravel 5.3 to laravel 5.5 and I am using php 7.1.11

On upgrading I am getting error

ErrorException (E_ERROR) Function mcrypt_get_iv_size() is deprecated

In config\app I have

'cipher' => 'AES-256-CBC'

Also try adding

error_reporting(E_ALL ^ E_DEPRECATED);

to it but still getting error.

$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, self::$skey, $text, MCRYPT_MODE_ECB, $iv);

This is code where I am using it.

2 Answers

Laravel has removed any mcrypt code in 5.3 (it was not used by default since Laravel 5.1) so I'm assuming this is OPs own code.

Ideally this code should be migrated to OpenSSL but until this happens it can be wrapped like so:

$olderrorReporting = error_reporting();
error_reporting($olderrorReporting&(~E_DEPRECATED));
$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, self::$skey, $text, MCRYPT_MODE_ECB, $iv);
error_reporting($olderrorReporting)

The above solution can be used to "suppress" deprecated warnings in general.

Sidenote: $a ^ $b is XOR in PHP so error_reporting(E_ALL ^ E_DEPRECATED) is basically equivalent to error_reporting(E_ALL)

Related