【问题标题】:PHP 7.4 direct replacement for mcrypt decryptionPHP 7.4 直接替代 mcrypt 解密
【发布时间】:2020-11-24 10:40:48
【问题描述】:

我有一个旧数据库,其内容是使用 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
}

而解密方法是这样的:

/**
 * 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
}

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

【问题讨论】:

  • 您能否添加一个示例加密数据,它是未加密的值,以及您问题的关键?
  • 事实证明,我被告知的数据不是用 mcrypt 编码的。看起来条目都是 MD5 哈希,所以即使我可以让 mcrypt 工作,也不会很容易解密!

标签: php encryption


【解决方案1】:

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

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

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

【讨论】:

  • 我在让它工作时遇到了问题,虽然我今天再次尝试并意识到我缺少 libmcrypt-dev 包:现在安装正确,所以也许我没问题。
【解决方案2】:

对于那些使用 cPanel 的人,你可以在 PHP 7.3 中简单地做到这一点
转到 PHP Selector,如果不是最新版本,请选择 7.3 PHP 版本,然后选择“mcrypt”和“sodium”扩展。

然后您可以在同一个 PHP 文件上使用两种加密,以便在一次操作中使用“mcrypt”解密您的数据并使用“sodium”加密。

【讨论】:

    猜你喜欢
    • 2011-08-09
    • 2018-08-02
    • 2017-01-03
    • 2011-12-26
    • 2011-01-24
    • 2015-11-10
    • 2013-01-20
    • 2011-08-16
    • 2017-06-04
    相关资源
    最近更新 更多