带PDO的忘记密码脚本(需要解密)


Forgotten Password script with PDO (decrypt needed)

我可能有一个愚蠢的问题。。。我正在做一个被遗忘的密码脚本(当然是登录系统),但我被卡住了。我用一个特殊的ID创建了一个代码(它正在工作,是的!),但我无法解密它…你能帮我吗?

这是我创建特殊ID的功能:

Recovery_Script.php

<?php
include "pdo.php"; if(isset($_POST["submit"]) AND isset($_POST["ForgotPassword"])) {
$email = $_POST["ForgotPassword"];
// Check to see if a user exists with this e-mail
$sql = "SELECT email FROM account WHERE email=:email";
$stmt = $db->prepare($sql);
$stmt->execute(array(":email"=>$email));
$items = $stmt->fetchAll();
$db = null;
 foreach($items as $data){
       if($data["email"] == $email){
        // Create a unique salt. This will never leave PHP unencrypted.
        $salt = "498#2D83B631%3800EBD!801600D*7E3CC13";
        // Create the unique user password reset key
        $password = hash('sha256', $salt.$email);
        // Create a url which we will direct them to reset their password
        $pwrurl = "http://student.sps-prosek.cz/~kocvaja14/Project/SelfMade/templates/script/recovery_password.php?q=".$password;
        // Mail them their key
        $mailbody = "Dobrý den,'n'nJestli tento email nepatří vám, prosím, ignorujte jej. Byla vytvořena žádost o obnovení hesla na webové stránce http://student.sps-prosek.cz/~kocvaja14/SelfMade/'n'nPro obnovení hesla klikněte na odkaz níže. 'n'nThanks,'nThe Administration";
        mail($email, "http://student.sps-prosek.cz/~kocvaja14/Project/SelfMade/index.php - Password Reset", $mailbody);
        echo "Your password recovery key has been sent to your e-mail address.";
} else
    echo "No user with that e-mail address exists.";  
      } }?>

现在我需要创建一个文件,在那里我将解密这个ID($password)。但我做不到(因为我对这些东西了解不多)。你能帮我吗?非常感谢。

无法解密sha哈希值。

您需要做的是放弃密码恢复的想法,而是构建一个密码重置系统。

这样做的一种方法是:

  1. 在用户表中,为"reset_hash"(一个很好的长散列字符串)添加一列
  2. 当用户请求重置密码时,构建一个散列值并将其存储在他们的记录中。然后,在电子邮件中,发送一个链接,其中包括作为url一部分的"reset_hash"
  3. 当用户遵循url时,请检查用户表中是否存在哈希。如果是,请提供一个表单,用户可以在其中更改密码

您应该考虑的其他预防措施可能包括使用NONCE和验证NONCE,这是导致链接在X小时后过期的一种方式(NONCE仅在一段时间内有效)。