0

I am trying to create a script to recover passwords by mail, but it does not work. Previously It came with an error that the e-mail was invalid. Now its ends me the mail but with the encrypted password in it.

Please help me to decrypt the password Any lead for this error is highly appreciable

  <?php
    require_once("config.php");    //Database Connection

    //Catch Field Data
    $email     =     $_POST['email'];
    $submitted    =    $_POST['submitted'];

    if($email) {
        $query        =    printf("SELECT * FROM registration where email='$email'");
        $result        =    mysql_query($query);
        $rowAccount    =    mysql_fetch_array($result);
    }

    if ($rowAccount) {

        $subject = "Your goprestige Username / Password Reminder";
        $headers = "From: info@abc.com";
        $fname = $rowAccount['fname'];
        $password = $rowAccount['password'];
        $msg = "<h1>My abc Admin</h1>
        <p>Hello '$user'!</p>
        <p>Here is the username/password reminder you requested. If you didn't request this reminder then don't panic too much, the likely hood of someone gaining access is minimal. Thank you abc </p>
        <p>Username: '$fname'</p>
        <p>Password: '$password'</p>
        <p>Many thanks, the abc Support Team.</p>
        ";

        $success = mail($email, $subject, $msg, $headers);

        if($success) {
            echo "<p id=errors>Reminder Success: Your Username and Password have been emailed to $email";
        }
    } else ($submitted) {
        echo '<p id="errors">Reminder Failed: The email you entered was not found on the system, please try again.</p>';
    }
?>

HTML

 <form class="form-horizontal ct-u-paddingBottom20" action="php/forget.php" method="post" id="passwd" style="display:none;">
                          <div class="form-group">
                                <label for="username" class="col-sm-2 control-label">Useremail: </label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="email" name="email" placeholder="enter your email id" required>
                                </div>
                            </div>
                          <div>
                              <center>
                                  <span style="color:green;display:none;" class="success-footer"><h4 style="margin-left: 0px;">password link is sent  </h4></span>
                                  <span style="color:orange;display:none;" class="error-footer"><h4 style="margin-left: 0px;">invalid email</h4></span>
                                </center>
                          </div>
                          <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-10">
                                     <p><input type="submit" value="Reset Password" /></p>
                                </div>
                            </div>


                       </form>

Javascript

<script type="text/javascript">

          var frm = $('#passwd');
          frm.submit(function (ev) {
              $.ajax({
                  type: frm.attr('method'),
                  url: frm.attr('action'),
                  data: frm.serialize(),
                  success: function (data) {
            //alert(data);
                      if (data) {
                        //alert('data');
                        $('.success-footer').css('display','block');
                      }
                      else{
                        $('.error-footer').css('display','block')
                      }
                  }
              });

              ev.preventDefault();

          });
      </script>
DarkBee
  • 16,592
  • 6
  • 46
  • 58
Tammy
  • 1,122
  • 5
  • 19
  • 50
  • What is the error message you have encounter? – Fil Apr 14 '16 at 04:42
  • Check you filter_var() function if perform well on email validation – Fil Apr 14 '16 at 04:48
  • Where is your form? You are doing `var frm = $('#passwd');`, so is your `
    ` or is that the `id` for your password field? Have you looked at your browser console to see what your ajax is posting, and if it is sending a valid `email` value?
    – Sean Apr 14 '16 at 04:51
  • print $email & check what it returns, it seems to me that it is blank. – Dipanwita Kundu Apr 14 '16 at 04:52
  • its the validation of email that if fall on if condition becomes true and not executing on else where sending email happens – Fil Apr 14 '16 at 04:53
  • @Sean yes my form id is "passw"... – Tammy Apr 14 '16 at 04:55
  • @DipanwitaKundu i tried with that too... in both the code but still same error – Tammy Apr 14 '16 at 05:01
  • .. Please don't use `MD5` to store passwords. Use [password_hash](http://php.net/manual/en/function.password-hash.php) to do this. Do note passwords are encrypted for a reason. Instead of trying to decrypt the current password you could just create and send a new password – DarkBee Apr 14 '16 at 06:17
  • so is by using password_hash it will send me the original password for that.? – Tammy Apr 14 '16 at 06:20
  • You "can't" decrpyt passwords. It is "impossible" and you shouldn't do it. Alter your script so a new password is created or your user gets the option to choose a password him/herself – DarkBee Apr 14 '16 at 06:24
  • okey thank you... @DarkBee – Tammy Apr 14 '16 at 06:31

3 Answers3

1

I'm assuming that your are using xampp to send the email. If that's the case, please follow the guidelines under in this discussion How to configure XAMPP to send mail from localhost? to configure your email settings.

here's the sample code

<?php
$Results['id'] = 1;
$message = "Your password reset link send to your e-mail address.";
$to      = 'your_email_id@gmail.com';
$subject = "Forget Password";
$from    = 'info@abc.com';
// $body    = 'Hi, <br/> <br/>Your Membership ID is '.$Results['id'].' <br><br>Click here to reset your password http://google.com/login-signup-in-php/reset.php?encrypt='.$encrypt.'&action=reset .';
$body    = "Hello";
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "Reply-To: ". strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

mail($to, $subject, $message, $headers);
?>

If you follow correctly the guidelines, try the given code above which basically from your code and removed temporarily some data which fetched from the database just to generate a test if mail is sending data. If successful, then it's the time you put it back to

if (condition) {
  # code...
} else {
  # code...
}

of your code.

Hope this help the problem

Community
  • 1
  • 1
Fil
  • 8,225
  • 14
  • 59
  • 85
  • i tried with one it sends me the password but as my password is in md5.. so its comes with encrypted password .. how to decrypt that. – Tammy Apr 14 '16 at 05:55
  • I see that's why invalid email address. This will help http://stackoverflow.com/questions/15194663/encrypt-and-decrypt-md5 – Fil Apr 14 '16 at 06:03
  • i have edited my new code which is working but still with encrypted code ..please do help me – Tammy Apr 14 '16 at 06:17
  • Still what is not working can you share the error you are getting; – itzmukeshy7 Apr 14 '16 at 06:32
  • @itzmukeshy7 as my code is encrypted in md5 so its comes with md5 code only... i want it to be original or cusomers can able to make a new passwrd for that. – Tammy Apr 14 '16 at 07:03
  • Got it; create a reset password logic then and don't share even password hash it can be decrypted; – itzmukeshy7 Apr 14 '16 at 07:07
1

Typically passwords are hashed, not encoded. The difference is that a hash cannot be "unhashed" to bring back the original password. In the password saving function of your code, checkif you used the password_hash() function. If you did, you can't get that password back.

That's actually a good thing because password reminders are generally an unsecure way of providing a "Forgot password" function. See Troy Hunt's article on building a secure password reset function (http://www.troyhunt.com/2012/05/everything-you-ever-wanted-to-know.html)

A couple of solutions could be:

1) Provide a one-time, time-limited use password that they can use to change their password. Ensure that only one of these are live at any moment.

2) Provide a link to a unique url that would allow the user to be challenged with something, such as a security question, to validate that they are not only the recipient of the email, but actually the person who requested the password change.

The second option would be more secure as it would provide an extra layer of validation at the cost, of course, of convenience to the user.

John Cruz
  • 147
  • 1
  • 9
-1

Update your jquery ajax like this:

$.ajax({
  method: "POST",
  .....
})

because method is default: 'GET'

Dipanwita Kundu
  • 1,637
  • 1
  • 9
  • 14
  • This is a comment, not an answer. – Sean Apr 14 '16 at 04:48
  • i tried with this new code it seems wrking for me.. but it sends me with encrypted md5 code.. how to change it with decrypted code in original code. – Tammy Apr 14 '16 at 06:16
  • there is no way to decrypt md5() data.instead of send md5 password, send random generated password & ask him to rest by his own or generate a link for user & ask him to create his own password. or create password using your salt so that you can easily decrypt it.. – Dipanwita Kundu Apr 14 '16 at 06:21
  • OP used this in `$.ajax({type: frm.attr('method'),});` both have same meaning; – itzmukeshy7 Apr 14 '16 at 06:30
  • @itzmukeshy7, yes, You should use type if you're using versions of jQuery prior to 1.9.0. For more details please follow the link :http://api.jquery.com/jquery.ajax/ – Dipanwita Kundu Apr 14 '16 at 06:31
  • okey thank you @DipanwitaKundu ...will try to do as per your instruction – Tammy Apr 14 '16 at 06:32
  • @DipanwitaKundu now `type` will work for every version but `method` not because `method` is added in 1.9.0; so be on safe side one should use `type` without any version consideration; – itzmukeshy7 Apr 14 '16 at 06:35
  • @DipanwitaKundu Creating your own salted password so you can decrypt them is such a bad advice. About the `MD5` comment that is not decryptable, which is true, but other [resources](https://www.freerainbowtables.com/tables2/) exist to retrieve MD5 hashes, so it better not to rely on MD5 anymore – DarkBee Apr 14 '16 at 06:55