mcrypt_compat
mcrypt_compat copied to clipboard
I have legacy system that has data already encrypt, Wanted to decrypt all inside php8
I try the code below , but does not working to decypt data that has been encrpyted during legacy system .I believe legacy system using php7.1
anyone could explain why ?
if (!function_exists('decrypt')) {
function decrypt($data, $key = '@bMp74Gx7k3E#2f6')
{
$module = @mcrypt_module_open('rijndael-256', '', MCRYPT_MODE_CBC, '');
$key = byteSubstr($key, 0, mcrypt_enc_get_key_size($module));
$ivSize = mcrypt_enc_get_iv_size($module);
$iv = byteSubstr($data, 0, $ivSize);
mcrypt_generic_init($module, $key, $iv);
$decrypted = mdecrypt_generic($module, byteSubstr($data, $ivSize, byteLength($data)));
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return rtrim($decrypted, "\0");
}
}
```
```
if (!function_exists('byteSubstr')) {
/**
* Extract substring based on byte length.
*
* @param string $string
* @param int $start
* @param int $length
* @param string $encoding
* @return string
*/
function byteSubstr($string, $start, $length = null)
{
if ($length === null) {
$length = byteLength($string);
}
return mb_substr((string)$string, $start, $length, '8bit');
}
}
```
```
if (!function_exists('byteLength')) {
/**
* Get the byte length of a string.
*
* @param string $string
* @return int
*/
function byteLength($string)
{
return mb_strlen((string)$string, '8bit');
}
}
```