Closed Thread
Results 1 to 8 of 8

Thread: Storing a Secure Password

  1. #1
    dirkfirst is offline Programming Expert
    Join Date
    May 2006
    Posts
    354
    Rep Power
    23

    Storing a Secure Password

    I'm creating a new database application (although I'm just winging my way through it). The application allows users to login and creates a session. Now, my question is what is the best way to store these user passwords in a database? Is there a one-way encryption method?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Dan
    Dan is offline Programmer
    Join Date
    Jun 2006
    Posts
    145
    Rep Power
    0
    Are you using PHP, or PHP combined with MYSQL?

  4. #3
    dirkfirst is offline Programming Expert
    Join Date
    May 2006
    Posts
    354
    Rep Power
    23
    PHP and MySQL

  5. #4
    Dan
    Dan is offline Programmer
    Join Date
    Jun 2006
    Posts
    145
    Rep Power
    0
    Pretty complex stuff and not something I have ever done custom, but I've fished out some info for you:

    MYSQL Password hashing - should be what you need?

    It really depends on what your business is and you must decide for yourself what lengths you need to go to for your users protection. The key is to take all reasonable measures to protect the data. This is where the fun of personal privacy laws come into play. Also remember that you are only legally allowed to require information from your user which you NEED to do business with them.

    So...

    Your user must enter said information and you are going to store it. How are you going to protect their information? You need the information to be accessible by the person who entered it and only the person who entered it. This would require some information that only the user knows. How can you acquire this information and how can you store it.

    HTTPS

    Again, this depends on what type of information you will be storing. The first thing involved is to get a secure certificate for your site and only transfer sensitive, or personal, user data over an encrypted connection. I have always gotten secure certs from Thawte and always been satisfied. One thing to note is the form itself does not need to be under https but any form action does. As long as the form action is https then the secure connection is established before any data is sent. I've spent a lot of time sniffing this scenario to be sure that it is 100% true.

    User Account Access

    There will need to be a way for users to access their account. Most often this will consist of a username and a password. Usernames should be unique. This will allow the username password combination to be unique and be the first line of protection against account hijacking. Depending on the type of data you are storing, two fields that make up your unique combination may not be enough but for our explanation here we will use only the two fields.

    password Protection

    I realize I am only now getting to the heart of your question but, in truth, all of these things play a prt in it.

    passwords should never be stored on your system in plain text or in a decryptable form. MD5 is a one way encryption and is an acceptable method of storing passwords. There is absolutely no need for your users passwords to be accessible by you or anyone who works for your organization. A password can always be reset by the user or by you or your employees. You must encrypt the password when it is received and then store the encrypted password in your database. This makes sure that the password is useless in the form in which it is accessed straight from the database.

    Your form that takes the password should post to script which does something similar to the following. Ensuring the username is unique and that the password is protected.
    Code:
    $sql = "select * from usertable where username='" . $_POST['username'] . "'"; 
    $result = mysql_query($sql); 
    if (mysql_num_rows($result) >= 1) { 
     $error = "please enter another username"; 
     include "userform.php"; 
     exit(); 
    } else { 
     $username = $_POST['username']; 
     $userpass = md5($_POST['userpass']); 
     $sql = "insert into usertable values('$username','$userpass')"; 
     mysql_query($sql); 
     include "postregister.html"; 
    }
    You now have a stored password which is useless to you and only usable to the user through your login form.

    User Login

    Sessions or cookies are good methods to keep your user logged in and to be able to recognize them in your scripts. You must put some thought into how you are going to do your authentication and how you are going to stop the ability to hijack active sessions or hijack cookies.

    A simple login script may go something like the following. Once again the form you use for your login should have an action that is under https or be under https itself. I will use a session based example.
    Code:
    session_start(); 
    $username = $_POST['username']; 
    $userpass = md5($_POST['userpass']); 
    $sql = "select * from usertable where username='$username' and password='$userpass'"; 
    $result = mysql_query($sql); 
    if (mysql_num_rows($result)!= 1) { 
     $error = "Login failed"; 
     include "loginform.php"; 
    } else { 
     $_SESSION['username'] = "$username"; 
     $_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; 
     // any other data needed to navigate the site or 
     // to authenticate the user can be added here 
     include "membersection.php"; 
    }
    User Authentication

    Now an important factor is to be able to reliably recognize the user once they have logged in and to make sure that user is using their own session. In our above example we included the ip of the user to add some extra security. An authentication script would need to be included at the top of the page on every single page inside the members section of your site.

    A simple authentication script could be as follows.
    Code:
    session_start(); 
    $newip = $_SERVER['REMOTE_ADDR']; 
    if (!isset($_SESSION['username']) ¦¦  
    empty($_SESSION['username']) ¦¦ $newip!= $_SESSION['ip']) { 
     include "logout.php"; 
    }
    All of the above scripts are very simple and greater means may need to be taken to protect and authenticate your users but those three scripts are the basis of a user management system. You would also need to provide a method for your users to reset and acquire their passwords if need be. passwords should always be reset in some random fashion and then the user should be forced to change it before they continue using your site.

  6. #5
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    Without reading all of dan's information (cuz im lazy) the best wat to store passwords is using the md5 encryption method. PHP even has its own function:
    md5()

    Code:
    <?php
    $unencrypted_password 
    "test";
    $encrypted_password md5($unencrypted_password);
    echo 
    $encrypted_password;
    ?>
    Thats a simple script that shows how the password "test" would be stored if you use md5 encryption.

  7. #6
    Dan
    Dan is offline Programmer
    Join Date
    Jun 2006
    Posts
    145
    Rep Power
    0
    Yes, the information I provided outlines md5 password encryption but a little bit more complex than sidewinders, correct code.

  8. #7
    crocodile Guest
    md5 twice is better for your password safe

  9. #8
    TkTech's Avatar
    TkTech is offline The Crazy One
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1,412
    Blog Entries
    1
    Rep Power
    31
    crocodile, md5 twice wont matter from md5 once. No one yet has broken md5 and the current brute forcers are bloated, slow, and absolutly pathetic.

    PS. crocodile I blanked your signature, mainly cause its md5 "cracking" site.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Storing names
    By vaironl in forum C and C++
    Replies: 20
    Last Post: 05-08-2011, 04:42 PM
  2. counting and storing
    By pindo in forum Java Help
    Replies: 6
    Last Post: 05-03-2011, 10:02 PM
  3. Storing Data?
    By espdev-darkness in forum C and C++
    Replies: 5
    Last Post: 11-19-2010, 08:30 PM
  4. Storing Data?
    By TcM in forum Software Security
    Replies: 11
    Last Post: 11-09-2007, 06:02 PM
  5. Storing Binary
    By NeedHelp in forum Database & Database Programming
    Replies: 3
    Last Post: 06-27-2006, 02:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts