问题
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