3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * MCrypt Library to enable encryption and decryption using MCrypt. * NOTE: this is an adjusted version of the below source. * SOURCE: https://raw.githubusercontent.com/serpro/Android-PHP-Encrypt-Decrypt/master/PHP/MCrypt.php */ class Mcrypt { /** * IV parameter */ private $iv = 'hoewhowhahwoahoe'; function __construct() { // empty } /** * @param string $str * @param bool $isBinary whether to encrypt as binary or not. Default is: false * @return string Encrypted data */ public function encrypt($str, $key, $isBinary = FALSE) { // variables $str = $isBinary ? $str : utf8_decode($str); $td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $this->iv); // guard: check if the str is valid if (empty($str)) { return $str; } mcrypt_generic_init($td, $key, $this->iv); $encrypted = mcrypt_generic($td, $str); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $isBinary ? $encrypted : base64_encode(bin2hex($encrypted)); } /** * @param string $code * @param bool $isBinary whether to decrypt as binary or not. Default is: false * @return string Decrypted data */ public function decrypt($code, $key, $isBinary = FALSE) { // variables $code = $isBinary ? $code : $this->hex2bin(base64_decode($code)); $td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $this->iv); // guard: check if the code is valid if (empty($code)) { return $code; } mcrypt_generic_init($td, $key, $this->iv); $decrypted = mdecrypt_generic($td, $code); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $isBinary ? trim($decrypted) : utf8_encode(trim($decrypted)); } protected function hex2bin($hexdata) { $bindata = ''; for ($i = 0; $i < strlen($hexdata); $i += 2) { $bindata .= chr(hexdec(substr($hexdata, $i, 2))); } return $bindata; } } $mcrypt = new Mcrypt(); $key = "8f1ec9b37ccd2e88aa4279999818d28d"; $string = "OGZkM2M2NGI4MWRlMzg1MzlmZDRiN2ZjODA1NTM1MDA="; echo($mcrypt->decrypt($string, $key));

preferences:
35.98 ms | 413 KiB | 5 Q