diff --git a/lib/mcrypt.php b/lib/mcrypt.php index c9213e5..8a98a2d 100644 --- a/lib/mcrypt.php +++ b/lib/mcrypt.php @@ -40,6 +40,7 @@ use phpseclib3\Crypt\RC4; use phpseclib3\Crypt\Random; use phpseclib3\Crypt\Common\SymmetricKey as Base; +use phpseclib3\Common\Functions\Strings; if (!defined('MCRYPT_MODE_ECB')) { /**#@+ @@ -107,6 +108,20 @@ } if (!function_exists('phpseclib_mcrypt_list_algorithms')) { + /** + * Returns the string length + * + * PHP8.1 emits a warning if $string isn't a string + * + * @param string $string + * @return int + * @access private + */ + function phpseclib_strlen($string) + { + return Strings::is_stringable($string) ? strlen($string) : 0; + } + /** * Sets the key * @@ -182,7 +197,7 @@ function phpseclib_set_iv(Base $td, $iv) { if ($td->getMode() != 'ecb' && $td->getMode() != 'stream') { $length = $td->getBlockLength() >> 3; - $iv = str_pad(substr($iv, 0, $length), $length, "\0"); + $iv = str_pad(substr(Strings::is_stringable($iv) ? $iv : '', 0, $length), $length, "\0"); $td->setIV($iv); } } @@ -647,10 +662,10 @@ function phpseclib_mcrypt_enc_self_test(Base $td) function phpseclib_mcrypt_generic_init(Base $td, $key, $iv) { $iv_size = phpseclib_mcrypt_enc_get_iv_size($td); - if (strlen($iv) != $iv_size && $td->getMode() != 'ecb') { - trigger_error('mcrypt_generic_init(): Iv size incorrect; supplied length: ' . strlen($iv) . ', needed: ' . $iv_size, E_USER_WARNING); + if (phpseclib_strlen($iv) != $iv_size && $td->getMode() != 'ecb') { + trigger_error('mcrypt_generic_init(): Iv size incorrect; supplied length: ' . phpseclib_strlen($iv) . ', needed: ' . $iv_size, E_USER_WARNING); } - if (!strlen($key)) { + if (!phpseclib_strlen($key)) { trigger_error('mcrypt_generic_init(): Key size is 0', E_USER_WARNING); return -3; }