Replace deprecated mcrypt_cbc with mcrypt_encrypt

时光总嘲笑我的痴心妄想 提交于 2019-12-13 02:18:54

问题


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)));
}

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

mcrypt_encrypt(): Module initialization failed

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


回答1:


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)));
}

Example:

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


来源:https://stackoverflow.com/questions/37250852/replace-deprecated-mcrypt-cbc-with-mcrypt-encrypt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!