There is no problem if the generated string is not the same every time it is generated, as long as you have a key
it is possible to decrypt it to the original string. Look at the following code:
<?php
function encriptar($key='', $cadena = ''){
$iv = mcrypt_create_iv(
mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC),
MCRYPT_DEV_URANDOM
);
$encrypted = base64_encode(
$iv .
mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
hash('sha256', $key, true),
$cadena,
MCRYPT_MODE_CBC,
$iv
)
);
return $encrypted;
}
function desencriptar($key='', $cadena = ''){
$data = base64_decode($cadena);
$iv = substr($data, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
$decrypted = rtrim(
mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
hash('sha256', $key, true),
substr($data, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)),
MCRYPT_MODE_CBC,
$iv
),
"$string = 'stack overflow';
// encriptar
$hash = crypt($string, 'st');
echo "<br>encriptado: ".$hash;
// verificar
if (crypt($string, $hash) == $hash) {
echo "<br>si, correcto!!";
}
"
);
return $decrypted;
}
$key = 'key para encriptar';
$string = 'stack overflow';
echo "Datos oginales:<br>key: ".$key."<br>cadena:".$string;
$encryptedstring = encriptar($key, $string);
echo "<br><br>cadena encriptada:".$encryptedstring;
$decryptedstring = desencriptar($key, $encryptedstring);
echo "<br><br>cadena desencriptada:".$decryptedstring;
?>
However this is not a good practice to encrypt passwords, besides being an old method (although it is not deprecated ).
If you want to encrypt a string and always get the same result when encrypting, try the following:
// forma 1:
$hash = password_hash('rasmuslerdorf', PASSWORD_DEFAULT);
// forma 2:
$options = [
'cost' => 11
];
$hash = password_hash('rasmuslerdorf', PASSWORD_BCRYPT, $options);
the crypt(str,salt)
function generates a constant key. see attributes here to generate salt correctly
But, if you want to have high security despite not always generating the same password, try password_hash and password_verify . First, to encrypt:
if (password_verify('rasmuslerdorf', $hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}
To decrypt:
<?php
function encriptar($key='', $cadena = ''){
$iv = mcrypt_create_iv(
mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC),
MCRYPT_DEV_URANDOM
);
$encrypted = base64_encode(
$iv .
mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
hash('sha256', $key, true),
$cadena,
MCRYPT_MODE_CBC,
$iv
)
);
return $encrypted;
}
function desencriptar($key='', $cadena = ''){
$data = base64_decode($cadena);
$iv = substr($data, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
$decrypted = rtrim(
mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
hash('sha256', $key, true),
substr($data, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)),
MCRYPT_MODE_CBC,
$iv
),
"$string = 'stack overflow';
// encriptar
$hash = crypt($string, 'st');
echo "<br>encriptado: ".$hash;
// verificar
if (crypt($string, $hash) == $hash) {
echo "<br>si, correcto!!";
}
"
);
return $decrypted;
}
$key = 'key para encriptar';
$string = 'stack overflow';
echo "Datos oginales:<br>key: ".$key."<br>cadena:".$string;
$encryptedstring = encriptar($key, $string);
echo "<br><br>cadena encriptada:".$encryptedstring;
$decryptedstring = desencriptar($key, $encryptedstring);
echo "<br><br>cadena desencriptada:".$decryptedstring;
?>
See Predefined Constants