After being educated yesterday about trying to hash my usernames I decided on using the AES_ENCRYPT and DECRYPT functions in MySQL to securely store my usernames. As it stands I'm able to encrypt and store them in my database with this:
$con=mysqli_connect($servername, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$stmt = $con->prepare("INSERT INTO users (username, password) VALUES (AES_ENCRYPT(?, '$aesKey'), ?)");
$stmt->bind_param("ss", $username, $password);
$username = $_POST['username'];
$plainPass = $_POST['password'];
$password = password_hash($plainPass, PASSWORD_ARGON2I);
$stmt->execute();
$stmt->close();
$con->close();
echo "<script> location.href='login.php'; </script>";
}
?>
I'm using a 256 bit encryption key which is loaded from a config file and is called as $aesKey
However, when I try and log in I'm unable to. Nothing happens. No SQL errors are printed but I do get PHP Notice: Undefined variable: username
. Here's the code:
$submittedUser = $_POST['username'];
$submittedPass = $_POST['password'];
$con=mysqli_connect($servername, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if ($stmt = mysqli_prepare($con, "SELECT * FROM users Where username =AES_DECRYPT(?, '$aesKey')")) {
mysqli_stmt_bind_param($stmt, "s", $submittedUser);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
echo mysqli_error($con);
}
while($row = mysqli_fetch_array($result))
{
$username = $row['username'];
$password = $row['password'];
}
if ($submittedUser == $username && password_verify($submittedPass, $password))
{
$_SESSION['user']=$username;
//echo "<script> location.href='index.php'; </script>";
// exit;
}
else
{
//echo "<script> location.href='login.php'; </script>";
// exit;
}
mysqli_close($con);
}
?>
I commented out the redirects as I was originally thinking they were what was causing the error messages not to be printed. Can anyone help me out? I can't seem to figure out what's not working.
Edit: I changed the statement back to "SELECT * FROM users Where username =AES_ENCRYPT(?, '$aesKey')"
and I receive the following output:
Array ( [0] => 5�uY��V�s~"�ܮ� [username] => 5�uY��V�s~"�ܮ� [1] => $argon2i$v=19$m=1024,t=2,p=2$akcuREppYk1ERjRlUi5ZQw$yIdC5oMblekARArcB0XjwwmWywJ824iM8FmKqAu+9ys [password] => $argon2i$v=19$m=1024,t=2,p=2$akcuREppYk1ERjRlUi5ZQw$yIdC5oMblekARArcB0XjwwmWywJ824iM8FmKqAu+9ys )`
Edit 2: Here is the database structure. The unencrypted username should be evan and the unhashed password should be edwards.
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`username` text NOT NULL,
`password` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`username`, `password`) VALUES
('5ÇuY¯úVõs~\"ÐÜ®€', '$argon2i$v=19$m=1024,t=2,p=2$NWZQS2Q3dE1ORndjaUdpWg$vOR3DBT+IhdAXIt7YUPDiExalf1lsMeIVe/zTZDDYfU');
Edit 3: Running the following code:
<?php
include 'config.php';
$con=mysqli_connect($servername, $dbusername, $dbpassword, $dbname);
$result = mysqli_query($con,"SELECT * FROM `users`");
while($row = mysqli_fetch_array($result))
{
echo $row['username'];
echo "<br>";
echo $row['password'];
}
mysqli_close($con);
?>
Gives me the following result:
5�uY��V�s~"�ܮ�
$argon2i$v=19$m=1024,t=2,p=2$NWZQS2Q3dE1ORndjaUdpWg$vOR3DBT+IhdAXIt7YUPDiExalf1lsMeIVe/zTZDDYfU