如何将 Mcrypt 迁移到 openssl 函数以使用 PHP7.3 进行加密

How to migrate Mcrypt to openssl function to encrypt with PHP7.3

不幸的是 7.3 不支持 mcrypt,我必须重构一些代码。 我无法迁移此加密函数:

mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, pkcs5_pad(trim($strToEncode), mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB)), MCRYPT_MODE_ECB, $iv);

我试过这段代码:

function pkcs5_pad ($text, $blocksize) 
{ 
   $pad = $blocksize - (strlen($text) % $blocksize); 
   return $text . str_repeat(chr($pad), $pad); 
} 

$cipher = "aes-128-gcm";
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher));
$block_size = strlen(openssl_encrypt('', $cipher, '', OPENSSL_RAW_DATA, $iv));
$ciphertext = openssl_encrypt(pkcs5_pad(trim($strToEncode),$block_size), $cipher, $key, $options=0, $iv);

但它不起作用,我得到:pkcs5_pad 函数中未捕获的 DivisionByZeroError

这是工作代码:

$cipher = "aes-128-ecb";
$ciphertext = openssl_encrypt(trim($strToEncode), $cipher, $key, OPENSSL_RAW_DATA);