final class Encryption {
private $key;
public function __construct($key) {
$this->key = hash('sha256', $key, true);
}
public function encrypt($value) {
return strtr(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, hash('sha256', $this->key, true), $value, MCRYPT_MODE_ECB)), '+/=', '-_,');
}
public function decrypt($value) {
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, hash('sha256', $this->key, true), base64_decode(strtr($value, '-_,', '+/=')), MCRYPT_MODE_ECB));
}
}
The above code does not work anymore in php 7.2 because of the mcrypt function. How can I convert it to some equivalent function in php 7.2?
PHP's mcrypt library was deprecated in favor of openssl.
I recommend using AES-CBC-256 along with openssl_encrypt
and openssl_decrypt
to replace MCRYPT_RIJNDAEL_256.
http://us2.php.net/manual/en/function.openssl-encrypt.php http://us2.php.net/manual/en/function.openssl-decrypt.php