用mcrypt_encrypt替换不推荐使用的mcrypt_cbc [英] Replace deprecated mcrypt_cbc with mcrypt_encrypt

查看:224
本文介绍了用mcrypt_encrypt替换不推荐使用的mcrypt_cbc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的算法来编码要与PHP 7一起使用的密码

I have an old algorithm to encode passwords which I want to use with PHP 7

public function encriptarPass($cadena)
{
     $extra = (strlen($cadena)%8);
     for ($i = $extra; $i < 8; $i++) {
        $cadena .= chr(8-$extra);
     }
     $key = "stack";
     $iv = "stack";                    

     return strtoupper(bin2hex(mcrypt_cbc(MCRYPT_3DES, $key, $cadena, MCRYPT_ENCRYPT, $iv)));
}

我尝试将mcrypt_cbc替换为mcrypt_encrypt,但出现此错误:

I tried to replace mcrypt_cbc with mcrypt_encrypt and I get this error:

mcrypt_encrypt():模块初始化失败

mcrypt_encrypt(): Module initialization failed

我想更新算法以使用存储在数据库中的旧密码. 我知道我应该使用bcrypt或其他算法,但目前我需要更新此旧算法

I want to update the algorithm to work with old passwords stored in the database. I know I should use bcrypt or another algorithm but for the moment I need to update this old algorithm

推荐答案

这两个函数将返回相同的结果.

These two functions will return the same.

function encriptarPass($cadena){
    $extra = (strlen($cadena)%8);
    for($i = $extra; $i < 8; $i++) {
        $cadena .= chr(8-$extra);
    }
    $key = "stack";
    $iv = "stack111";
    return strtoupper(bin2hex(mcrypt_cbc(MCRYPT_3DES, str_pad($key, 24, "\0"), $cadena, MCRYPT_ENCRYPT, $iv)));
}

function encriptarPass2($cadena){
    $extra = (strlen($cadena)%8);
    for($i = $extra; $i < 8; $i++) {
        $cadena .= chr(8-$extra);
    }

    $key = "stack";
    $iv = "stack111";
    return strtoupper(bin2hex(mcrypt_encrypt(MCRYPT_3DES, str_pad($key, 24, "\0"), $cadena, MCRYPT_MODE_CBC, $iv)));
}

示例:

echo encriptarPass('test987x'); // Writes 10C9B50682CC21909AC4346CDFC4586E
echo encriptarPass2('test987x'); // Writes 10C9B50682CC21909AC4346CDFC4586E

这篇关于用mcrypt_encrypt替换不推荐使用的mcrypt_cbc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆