用mp7.3的mcrypt替代rijndael-256

问题描述

我在将Contao4与PHP7.0结合使用时,想升级PHP7.3,但是mcrypt扩展名被删除,因此Encryption类不再起作用。如弃用消息中所述,您应该使用openssl或PHPseclib。我用openssl尝试过,但是似乎无法处理rijndael-256,所以我想我可以用PHPseclib重写它。基本上这是旧代码的工作方式:

$key = 'd21c1a11ee5abf6048fb48bd5e7950813fd311d21b42a014ce52b7dd0ea351e0';
$varValue = 'PuseQIP3azjvmoP3O559x8IVjuvvtq5I6DtJEMZXWI5ZXBr3tSvbREba';

function decrypt_old($varValue,$key) {
    $resTd = mcrypt_module_open('rijndael-256','','cfb','');
    $varValue = base64_decode($varValue);
    $ivsize = mcrypt_enc_get_iv_size($resTd);
    $iv = substr($varValue,$ivsize);
    $varValue = substr($varValue,$ivsize);
    
    mcrypt_generic_init($resTd,md5($key),$iv);
    $strDecrypted = mdecrypt_generic($resTd,$varValue);
    mcrypt_generic_deinit($resTd);
    return $strDecrypted;
}

echo decrypt_old($varValue,$key)."\n";

现在使用PHPseclib创建新类:

use PHPseclib\Crypt\Rijndael;

class Encryption
{
    private const IV_SIZE = 32;
    private const MODE = Rijndael::MODE_CFB;
 
    public static function decrypt(string $value,string $encryptionKey): string
    {
        $value = base64_decode($value);
        $iv = substr($value,self::IV_SIZE);
        $value = substr($value,self::IV_SIZE); 

        $cipher = new Rijndael(self::MODE);
        $cipher->setBlockLength(32);
        $cipher->setKeyLength(32);
        $cipher->setKey(md5($encryptionKey));
        $cipher->setIV($iv);
        $cipher->disablePadding();
        return $cipher->decrypt($value);
    }
}

我认为问题在于mcrypt_get_block_size和mcrypt_get_key_size都返回32,在新的实现中,我将密钥长度和块长度设置为32,但是它们仍将设置为128($ cipher-> getBlockLength())。 知道如何实现这一点,以使输出与旧的实现相同吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)