Algorithm and implementation of protecting user's password

Source: Internet
Author: User
Tags hash log md5
Algorithm


My first response was to tell them to use the Secure Sockets Layer (secure Sockets layer,ssl) when they asked me how I could protect the user's login password on their site by using JavaScript. SSL is the best solution for security-sensitive Web applications when used correctly. However, some developers prefer not to use SSL when they publish Web programs that are less sensitive to security.

Web programs use a technology called session state management to track the interaction between browsers and servers, and as the Hypertext Transfer Protocol requires, each browser's request is independent of other browsers, and a Web application must use some skill such as cookies, hidden table fields, or URL overrides to identify their specific session with a particular browser. Most server-side development environments such as asp,php,coldfusion use cookies.

The problem with session state management is that it is fundamentally unsafe, a hacker can intercept a cookie, a table field value, or a URL between the server and the user's browser to manage session state, and he can use that information to take over the user's session once it succeeds! (Translator's note: A typical example is a bid by impersonating a trading user)

Most server-side scripting development environments allow you to reduce this risk, for example, you can specify a very short expiration time for cookies and use unpredictable session-state information. However, the safest thing to do is to use Secure Sockets Layer (SSL), and after buying it, you can protect not only the user password but also the session state management information.

If you don't use SSL, it's a good idea to have users authenticate to each other in the security-sensitive pages of your program, but from the user's point of view, it's just too much trouble. Finally, you must consider how much risk you and your users are willing to take. If the risk of password exposure is high, use SSL to build your program, and if you can't use SSL, then you'll be using the MD5 login process. This will at least protect your user's password. In addition, select a server-side scripting technology that allows you to protect session-state information.

Typically, when a user logs on without SSL (that is, normal HTTP), his or her password is always exposed in clear text from the time they leave the browser until they reach the server.

However, with the help of a mathematical function called a one-way function, we can design a login plan that does not expose the user's password. A function is a method that maps the elements of set A to set B, and the elements in each set a correspond to a certain element in set B. The function of a one-way function is to make the inverse of the calculation very difficult, that is, given a set of B elements, it is difficult to determine from the set of the element (if any) is mapped to the B set of this element.

A good analogy is the paper shredder, which is convenient to shred the paper in the Shredder, and it is quite difficult to reconstruct the original file from the fragments.

< Adelman Shamir, Rivest is the famous RSA encryption algorithm of the three inventors: Rivest, Rivest (Ronald digest abbreviation, MD5 is a 128-bit hash algorithm), by Ronald A very popular and widely used one-way function application is the MD5 algorithm (translator NOTE: MD is message>

We are now using the MD5 algorithm to develop a process that will be able to protect the passwords that are delivered from the browser to the server.

When a user attempts to log on to a Web application, the server-side script provides the user with a table containing random numbers from billions of possible values, and when the user enters their IDs and passwords in this login form, the client's script adds the random value to the password and MD5 the one-way operation. Then the output of the MD5 algorithm as the password, I will call this password MD5 password.

The client script then transmits the user ID and MD5 password to the server, because this value is the output of the MD5 algorithm, and it is almost impossible to extrapolate the password that the user originally entered. (Translator Note: The MD5 algorithm has been breached by the German decryption experts record, in the high security requirements of the use of the situation is still a risk)

When the user ID and MD5 algorithm's password is received by the Web application, the Web program performs the same operation as the user's browser, which adds the random number that was originally passed to the user to the user's password (this password is taken from the protected area of the server) and then calculates the correct MD5 password value. Compare this value to the value received from the browser, such as equality, set a server-side session variable to determine the user authentication pass.

At this point you may wonder why you use random numbers, random numbers are used to prevent a new attack, if only the user password after MD5 operation, this MD5 password will always maintain a value, listening to hackers simply intercept the MD5 password can be used to log into the Web application. This random number ensures that each new login has a specific MD5 password.

The MD5 landing scheme is relatively easy to use with JavaScript, and a full implementation of an open source can be obtained from Paul Johnston. (Note: see appendix)

We now implement server-side scripting with ASP (because it supports Jscript,microsoft version of JavaScript). You can of course use any other server-side script, except that you have to translate the MD5 algorithm into that language.
The following is an ASP implementation of the login form, Login.asp:

$#@60;%@ LANGUAGE = "JScript"%$#@62;
$#@60; html$#@62;
$#@60; head$#@62;
$#@60; title$#@62; Please log in!$#@60;/title$#@62;
$#@60;% session ("sharedvalue") = Math.random (). toString ()%$#@62;
$#@60; SCRIPT language= "JavaScript" src= "Md5.js" $#@62;$#@60;/script$#@62;
$#@60; SCRIPT language= "JavaScript" $#@62;

var sharedvalue = "$#@60;% =session (" Sharedvalue ")%$#@62;"

function Handlelogin () {
Sendmd5value (Calculatemd5value ())
}

function Calculatemd5value () {
var pw = document.forms["Login"].elements["password"].value
PW + + Sharedvalue
return calcMD5 (PW)
}

function Sendmd5value (hash) {
document.forms["Login"].elements["password"].value = Hash
document.forms["Login"].submit ()
}

$#@60;/script$#@62;
$#@60;/head$#@62;
$#@60; body$#@62;

$#@60; FORM name= "Login" method= "POST" action= "checkpassword.asp" $#@62;
User ID: $#@60;input type= "TEXT" name= "userid" size= "" $#@62;$#@60;br$#@62;
Password: $#@60;input type= "Password" name= "Password" size= "" $#@62;$#@60;br$#@62;
$#@60;input type= "button" Name= "Startlogin" value= "Login" $#@62;
$#@60;/form$#@62;

$#@60;/body$#@62;
$#@60;/html$#@62;

Only three of the above lines are ASP scripts (ASP scripts are surrounded by $#@60;% and%$#@62;).

On the server side, a script named checkpassword.asp validates the user ID and MD5 password:

$#@60;%@ LANGUAGE = "JScript"%$#@62;
$#@60;! --#include file = "Md5.inc"--$#@62;
$#@60;%
function Calculatemd5value () {
var pw = "" + Application (Request.Form ("userid"))
PW + + Session ("Sharedvalue")
Return calcMD5 ("" +p br>}
Clientpassword = Request.Form ("password")
Serverpassword = Calculatemd5value ()
if (Clientpassword = = Serverpassword) Response.Redirect ("Page1.htm")
else Response.Redirect ("tryagain.htm")
%$#@62;

The core of the authentication process is implemented by the four-line statement following the Calculatemd5value () function.

When implementing this scenario, simply change the first page of page1.html for the application you want to protect, and if you do not use ASP, translate the above ASP into your server-side scripting language.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.