I used a following method to Encrypt and Decrypt data between a php application and android app, It was working fine in php 5.6, After upgrading to php 7.2 it stopped working, Now I have moved back to php 5.6 temporarily. I know php mcrypt is depreciated. Now my problem is I cannot give update to Android App, I have to fix this somehow in the server side, I have predefined key and iv , Both in server side as well as app, What do I do so that I can use same key and iv and encrypt and decrypt data on sever side in php 7.2. Thanks in advance
class MCrypt
{
private $key = 'Some Key';
private $iv = 'Some IV';
function __construct()
{
}
function encrypt($str) {
//$key = $this->hex2bin($key);
$iv = $this->iv;
$td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
mcrypt_generic_init($td, $this->key, $iv);
$encrypted = mcrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return bin2hex($encrypted);
}
function decrypt($code) {
//$key = $this->hex2bin($key);
$code = $this->hex2bin($code);
$iv = $this->iv;
$td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
mcrypt_generic_init($td, $this->key, $iv);
$decrypted = mdecrypt_generic($td, $code);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return 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;
}
}