-1

I have migrated from PHP 5.x to PHP 7.3 and I am unable to understand how to fix this function I use extensively:

function md5_decrypt($enc_text, $salt, $iv_len = 16)
{
   $enc_text = base64_decode($enc_text);
   $n = strlen($enc_text);
   $i = $iv_len;
   $plain_text = '';   
   $iv = substr($salt ^ substr($enc_text, 0, $iv_len), 0, 512);
   while ($i < $n) {
       $block = substr($enc_text, $i, 16);
       $plain_text .= $block ^ pack('H*', md5($iv));
       $iv = substr($block . $iv, 0, 512) ^ $salt;
       $i += 16;
   }
   return preg_replace('/\\x13\\x00*$/', '', $plain_text);
}

PHP 7.3 gives me the "PHP Warning: A non-numeric value encountered in line 7" error:

$iv = substr($salt ^ substr($enc_text, 0, $iv_len), 0, 512);

I understand that what substr() is returning is a string and must be translated into a numeric variable, but how to do it without breaking the code?

Any suggestions are very welcome!

Thank you in advance to everyone.

All the best, Fab.

Fabrizio Ferrari
  • 869
  • 2
  • 12
  • 25
  • Duplicate of [How do I convert a string to a number in PHP?](https://stackoverflow.com/questions/8529656/how-do-i-convert-a-string-to-a-number-in-php) – Guy Incognito Nov 28 '20 at 16:17
  • can you show us how and with which parameters you call your function? – SaschaP Nov 28 '20 at 16:25
  • where have you sourced that function from? – hakre Nov 28 '20 at 17:52
  • Thank you guys for your replies. @GuyIncognito Yes, this is definitively a casting related issue, but I don't know how to fix it, hence my posting here. – Fabrizio Ferrari Nov 29 '20 at 18:52
  • @hakre Of course, sorry I forgot to show you an example. This function is called to simply decode passwords or other sensitive data, and it is called this way: $decoded_password = md5_decrypt($encoded_password, $salt); | $salt may be whatever you like, and must be the same of the opposite function which I haven't posted above, but works in the opposite way. $salt can be considered the "key" to encode and decode. Where did I find that function? I can't recall, that was several years ago... awaiting fo your thoughts! Thank you again. – Fabrizio Ferrari Nov 29 '20 at 18:54
  • it would be good, if you could edit your question and add every detail, like the description how you call your function. – SaschaP Nov 30 '20 at 08:48
  • it seems that `substr` throws the exception because of the first argument `$salt ^ substr(...)`, if you split up the line you can verify this. Can you show us the output of `var_dump($salt)`? it seems to me that this is a numeric value and not a string... – SaschaP Nov 30 '20 at 08:54
  • 1
    So sorry guys, I have realized now that for some reason the $salt variable was null (!!), hence the error! Filled it with the right value (which is a string) and now everything works fine. Thank you for your help! – Fabrizio Ferrari Nov 30 '20 at 17:08

1 Answers1

0

So sorry guys, I have realized now that for some reason the $salt variable was null (!!), hence the error! Filled it with the right value (which is a string) and now everything works fine. Thank you for your help anyway!

Fabrizio Ferrari
  • 869
  • 2
  • 12
  • 25