PHP 7.4直接替换mcrypt解密

Pet*_*ord 2 php encryption

我有一个遗留数据库,其内容是使用 DES 通过 mcrypt 加密的(是的,我知道,那是很久以前的事了)加密方法如下:

/**
 * General encryption routine for generating a reversible ciphertext
 * @param String $string the plain text to encrypt
 * @param String $key the encryption key to use
 * @return String the cypher text result
 */
function encrypt($string, $key)
{
    srand((double) microtime() * 1000000);
    /* Open module, and create IV */
    $td = mcrypt_module_open('des', '', 'cfb', '');
    $ksub = substr(md5($key), 0, mcrypt_enc_get_key_size($td));
    $iv_size = mcrypt_enc_get_iv_size($td);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    /* Initialize encryption handle */
    if (mcrypt_generic_init($td, $ksub, $iv) != -1)
    {
        /* Encrypt data */
        $ctxt = mcrypt_generic($td, $string);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $ctxt = $iv . $ctxt;
        return base64_encode($ctxt);
    } //end if
}
Run Code Online (Sandbox Code Playgroud)

解密方法是这样的:

/**
 * General decryption routine for recovering a plaintext
 * @param String $string the cypher text to decrypt
 * @param String $key the encryption key to use
 * @return String the plain text result
 */
function decrypt($string, $key)
{
    $ptxt = base64_decode($string);
    /* Open module, and create IV */
    $td = mcrypt_module_open('des', '', 'cfb', '');
    $ksub = substr(md5($key), 0, mcrypt_enc_get_key_size($td));
    $iv_size = mcrypt_enc_get_iv_size($td);
    $iv = substr($ptxt, 0, $iv_size);
    $ptxtsub = substr($ptxt, $iv_size);
    /* Initialize encryption handle */
    if (mcrypt_generic_init($td, $ksub, $iv) != -1)
    {
        /* Encrypt data */
        $ctxt = mdecrypt_generic($td, $ptxtsub);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $ctxt;
    } //end if
}
Run Code Online (Sandbox Code Playgroud)

我需要在 PHP7.4 环境中提取这些数据,即使只是用更好的东西重新加密它,但我不确定如何使用 PHP7.4 中存在的东西(如钠)重现 mcrypt 操作。我想一种方法是启动某种仍然具有 mcrypt 的遗留 PHP 安装并离线执行,但是有没有更直接的方法来编码解密方法?

Jos*_*sef 6

虽然 mcrypt 不再是 PHP 的一部分(有充分的理由),但它仍然作为模块存在,您可以为 PHP 7.4 安装

https://pecl.php.net/package/mcrypt

安装它,确保重新加密所有数据,更新所有旧数据后,更改代码以不再使用它并删除扩展。