我为android制作了一个完整的注册/登录系统,一切都是无线和服务器端数据库,问题是现在我的密码被直接保存到数据库中(没有ecnryption)我搜索了大量的网站和SoF帖子和帖子,但实际上并没有帮助我。
我读到了关于md5和sha1,河豚和bcrypt但是无法理解这种密码学的正确实施。
使用php请求建立连接。注册码如下:
if($_SERVER['REQUEST_METHOD']=='POST'){
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$Firstname = $_POST['fname'];
$Lastname = $_POST['lname'];
$Date = $_POST['Date'];
require_once('db_config.php');
$check_username_email = "SELECT * FROM Users WHERE Username ='$username' AND Email = '$email'";
$check_u_e = mysqli_fetch_array(mysqli_query($db,$check_username_email));
$check_username = "SELECT * FROM Users WHERE Username ='$username'";
$check_u = mysqli_fetch_array(mysqli_query($db,$check_username));
$check_email = "SELECT * FROM Users WHERE Email ='$email'";
$check_e = mysqli_fetch_array(mysqli_query($db,$check_email));
if(isset($check_u_e)){
echo 'Username AND Email already exist, please change them Both.';
}
else if(isset($check_u)){
echo 'Username already exists, please change it.';
}
else if (isset($check_e)){
echo 'Email already exists, please change it.';
} else{
$sql = "INSERT INTO Users(Email,Username,Bio,Password,Fname,Lname,Regiser_Date) VALUES ('$email','$username',NULL,'$password','$Firstname','$Lastname','$Date')";
if(mysqli_query($db,$sql)){
echo "You have been successfully Registered";
} else{
echo "Sorry, something went wrong - Try again.";
}
}
}else{
echo 'Server error - Please try again later.';
mysqli_close($db);
}
我的登录信息在这里:
if($_SERVER['REQUEST_METHOD']=='POST'){
$username = $_POST['username'];
$password = $_POST['password'];
require_once('db_config.php');
$sql = "SELECT * FROM Users WHERE Username = '$username' AND Password = '$password'";
$result = mysqli_query($db,$sql);
$check = mysqli_fetch_array($result);
if(isset($check)){
echo 'Logged In';
}
else{
echo 'Failed to login';
}
}
除了加密,一切都很好。有人可以指导我完成这个吗?我读到它需要2个方面,一个来自客户端(android app),另一个来自php服务器端
答案 0 :(得分:2)
要加密密码,您可以使用password_hash功能。 在您的注册码中,在插入查询之前添加以下内容:
$options = [
'cost' => 12,
];
$password = password_hash($password, PASSWORD_BCRYPT, $options);
这将使用BCRYPT算法加密在注册时通过POST数组恢复的密码。 BCRYPT算法生成60个字符哈希(将输入字符串切割为70个字符),因此,在您的数据库中,您需要有一个CHAR(60)密码字段来存储哈希密码。
你也可以在$ options中添加自己的salt,如php文档中所述
强烈建议您不要为此功能生成自己的盐。如果您没有指定,它将自动为您创建安全盐。 如上所述,在PHP 7.0中提供salt选项将生成弃用警告。在将来的PHP版本中,可以删除对手动提供salt的支持。
现在您已在数据库中使用了加密密码,在登录脚本中,您需要从数据库中检索散列密码,然后将其与输入的password_verify()函数密码进行比较:
$sql = "SELECT password FROM Users WHERE Username = '$username'";
$result = mysqli_query($db,$sql);
if(!$row = mysqli_fetch_assoc($result)){
echo "failed to login(invalid username)";
die();
//login failed->add code to redirect to login page
}
if(password_verify($password, $row['password'])){
//password match the stored hash
echo "logged in";
//sucessfully logged->retrive all additional data you need, save session, redirect to homepage etc...
}
else echo "failed to log in(invalid password)";
您将找到有关password_hash()和password_verify()here的所有信息,但请考虑:
答案 1 :(得分:0)
所以它会像这样,在DB中存储密码,使用任何散列或加密,如bcrypt,md5,sha1并将其存储在数据库中。 当用户想要登录时,再次使用相同的散列或加密机制比较它,存储在DB中的值与用户传递的值匹配。 如果是,那就成功登录。
现在,
您对insert的查询将会出现类似这样的内容,
$sql = "INSERT INTO Users(Email,Username,Bio,Password,Fname,Lname,Regiser_Date) VALUES ('$email','$username',NULL,'md5($password)','$Firstname','$Lastname','$Date')";
并且
$sql = "SELECT * FROM Users WHERE Username = '$username' AND Password = 'md5($password)'";
然后使用Bcrypt,
中显示了一个示例
$options = [
'cost' => 12,
];
echo password_hash("password", PASSWORD_BCRYPT, $options)."\n";