【问题标题】:PHP: base64_encode didnt decrypt correct *EDIT*PHP:base64_encode 没有正确解密 *EDIT*
【发布时间】:2017-10-16 03:54:37
【问题描述】:

已编辑

我有一个带有登录表单的 Android 项目。我也有一些用户存储在服务器的数据库中,我正在尝试创建一个连接,以便使用 PHP 文件登录。我的问题是,即使我每次连接到一个已经存在的用户,我也会得到Login credentials are wrong. Please try again!,这是我尝试使用错误凭据登录时应该收到的错误消息。不知何故,它传递了错误的参数。我有下面的代码。

login.php

<?php
require_once 'DB_Functions.php';
require_once 'DB_Connect.php';
$db = new DB_Functions();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['email']) && isset($_POST['password'])) {

    // receiving the post params
    $email = $_POST['email'];
    $password = $_POST['password'];

    // get the user by email and password
    $user = $db->getUserByEmailAndPassword($email, $password);

    if ($user != false) {
        // user is found
        $response["error"] = FALSE;
        $response["oid"] = $user["oid"];
        $response["user"]["name"] = $user["name"];
        $response["user"]["surname"] = $user["surname"];
        $response["user"]["country"] = $user["country"];
        $response["user"]["email"] = $user["email"];
        $response["user"]["password"] = $user["password"];
        $response["user"]["salt"] = $user["salt"];
        $response["user"]["telephone"] = $user["telephone"];
        echo json_encode($response);
    } else {
        // user is not found with the credentials
        $response["error"] = TRUE;
        $response["error_msg"] = "Login credentials are wrong. Please try again!";
        echo json_encode($response);
    }
} else {
    // required post params are missing
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameters email or password are missing!";
    echo json_encode($response);
}
?>

以及 DB_Functions.php 上的 getUserByEmailAndPassword 函数

public function getUserByEmailAndPassword($email, $password) {

$stmt = $this->conn->prepare("SELECT * FROM owner WHERE email = ?");
$stmt->bind_param("s", $email);

if ($stmt->execute()) {
    $stmt->bind_result($user['oid'], $user['name'], $user['surname'], $user['country'], $user['email'], $user['password'], $user['salt'], $user['telephone']);
    while ($stmt->fetch()) {
            //printf("%s %s\n", $email, $password);
    }

    $stmt->close();

    // verifying user password
    $salt = $user['salt'];
    $encrypted_password = $user['password'];

    $hash = $this->checkhashSSHA($salt, $password);
    // check for password equality
    if ($encrypted_password == $hash) {
        // user authentication details are correct
        return $user;
    }
} else {
    return NULL;
}
}

我也使用 base64 加密。可能是加密功能有问题。这是我在存储用户时使用的 base64_encode 函数。它创建一个 10 位的盐并将其存储到每个用户的数据库中。一个示例盐是2b67fd277b。我知道盐的正确格式类似于cRDtpNCeBiql5KOQsKVyrA0sAiA=。为什么我会得到这种盐?

 public function hashSSHA($password) {

    $salt = sha1(rand());
    $salt = substr($salt, 0, 10);
    $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
    $hash = array("salt" => $salt, "encrypted" => $encrypted);
    return $hash;
}

还有解码

public function checkhashSSHA($salt, $password) {

    $hash = base64_encode(sha1($password . $salt, true) . $salt);

    return $hash;
}

我在这里错过了什么吗? 请帮忙,谢谢!

【问题讨论】:

  • 你真的不应该在密码哈希上使用你自己的盐,你真的应该使用 PHP 的 built-in functions 来处理密码安全。确保你 don't escape passwords 或在散列之前对它们使用任何其他清理机制。这样做会更改密码并导致不必要的额外编码。
  • 我不使用自己的盐,它是由内置函数自动生成的。如果你想检查,我可以发布它们
  • 我已经仔细检查过了,我没有逃避密码或在任何地方更改密码。如何检查加密是否正确?

标签: php encryption base64 salt


【解决方案1】:

找到了!我在存储用户时使用$password 而不是$encrypted_password,因此无法正确解密。最后在数据库中,密码列中不是真正的密码,而是一个加密字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    相关资源
    最近更新 更多