1
Hello, I’m trying to learn how to encrypt data. I created a code php
to save the data in the database mySQL
and another to compare user input data with the database.
To encrypt I used the function crypt()
. The problem is that when I do the comparison is generated a new encrypted password that does not match the bank, how to solve this? My version of php is 5.3.4
.
<?php
//cadastramento
$senha = $_POST["senha"];
//criptografar senha
$cript_senha = crypt($senha);
$sql = "SELECT senha FROM administrador";
//cadastrar administrador
$sql = "INSERT INTO administrador (senha)
VALUES ('$cript_senha')";
if (mysqli_query($conn, $sql)) {
print "Registrado com sucesso!";
} else {
print "Error: " . $sql . "<br>" . mysqli_error($conn);
}
?>
<?php
//entrada do usuário
$senha = $_POST["senha"];
$sql = "SELECT senha FROM administrador";
//quando faço a comparação não da certo,
//pois é gerada uma nova
//senha que não confere com a do banco
if ((crypt($senha, $row["senha"])) == $row["senha"]) {
print "Senhas idênticas";
} else {
print "Senhas diferentes ";
}
?>
According to the documentation, the second parameter is a salt, and you are passing the password ja from the bank. remove the
$row["senha"]
if and see if it works.– user28595
Der a
var_dump
in the array$rows
if he actually has the indexsenha
check in your database if the column sizesenha
has a size large enough to store the encrypted password, recommend leaving the column with avarchar(255)
. 'Cause the bank can crashtruncando
(breaking) the value and so the validation does not check.– Leonardo
@Diegofelipe the
if
of it is correct, so is the validation, in thesalt
would be the encrypted password.– Leonardo