2017-05-16 3 views
0

私は、ログインフォームとAndroidのプロジェクトを持っているPHP:BASE64_ENCODEは解読didntの正しい* EDIT *

編集しました。私はまた、いくつかのユーザーがサーバーのDBに格納されていると私はPHPファイルを使用して、ログインするように、接続を作成しようとしています。私の問題は毎回、すでに存在しているユーザーと接続しても、私はLogin credentials are wrong. Please try again!を取得しています。間違った資格情報でログインしようとするとエラーメッセージが表示されます。何とか間違ったパラメータを渡します。私は以下のコードを持っています。

DB_Functions.php

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; 
} 
} 

上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); 
} 
?> 

そしてgetUserByEmailAndPassword機能私はまた、base64で暗号化を使用します。多分、暗号化機能に何か問題があります。ここに私のbase64_encode関数は私がユーザーを格納するときに使用します。それは10桁の塩を作成し、各ユーザーのDBに格納します。塩の例は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; 
} 

私はここで何かが足りないのですか? 助けてください、ありがとう!

+1

あなたは本当にパスワードハッシュに自分の塩を使うべきではありません。本当にPHPの[組み込み関数](http://jayblanchard.net/proper_password_hashing_with_PHP.html)を使ってパスワードセキュリティを処理するべきです。ハッシュする前に[パスワードをエスケープしない](http://codereview.stackexchange.com/questions/79668/login-with-password-hash)か、他のクレンジングメカニズムを使用してください。パスワードを変更すると、パスワードが変更され、不要な追加のコーディングが発生します。 –

+0

自分の塩は使用しません。組み込み関数によって自動生成されます。 – Kzaf

+0

私はそれをダブルチェックして、私はパスワードをエスケープしたり、どこにでも変更したりしていません。暗号化が正しいかどうかをどうすれば確認できますか? – Kzaf

答えて

0

見つけました!私は$encrypted_passwordの代わりに$passwordを使用していたので、復号化を正しく行うことができません。最終的には、パスワード列の実際のパスワードのDBのisteadには、暗号化された文字列があります。

関連する問題