JavaScript
  Home arrow JavaScript arrow Page 4 - Building a CHAP Login System: Encrypting D...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Building a CHAP Login System: Encrypting Data in the Client
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 25
    2005-08-29

    Table of Contents:
  • Building a CHAP Login System: Encrypting Data in the Client
  • The basics of a CHAP login system: pros and cons of client-side data encryption
  • The making of a CHAP system: implementing a basic authentication mechanism
  • Completing the client code: defining the remaining JavaScript functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building a CHAP Login System: Encrypting Data in the Client - Completing the client code: defining the remaining JavaScript functions


    (Page 4 of 4 )

    To finish writing the required JavaScript code, the next thing to be done is define the “doCHAP()” function, which will perform the MD5 hash of the given password, then append to it the challenge string sent by the server, and finally apply again the MD5 algorithm on both concatenated values. Additionally, rough data validation will be performed trough the “showError()” function. With reference to this, here is the list of relevant functions:

    /*

     * verify form values &

     * implement the Challenge Handshaking Authentication Protocol

     */

    function doCHAP(){

      // get 'userid' field

     var usrid=document.getElementById('userid');

     if(!usrid){return};

      if(!usrid.value){showError(usrid,'Enter your ID');return false};

      // get 'password' field

      var psw=document.getElementById('passwd');

      if(!psw){return};

      if(!psw.value){showError(psw,'Enter your password');return false};

      // get 'challenge' field

      var chlng=document.getElementById('challenge');

      if(!chlng){return};

      // calculate MD5 hash of password and concatenate challenge value

      // next calculate MD5 hash of combined values

      chlng.value=MD5(MD5(psw.value)+'<?php echo $_SESSION['challenge']?>');

      // clear password field

      psw.value='';

      return true;

    }

    /*

     * display error messages

     */

    function showError(obj,message){

      alert(message);

      obj.focus();

    }

    As you can see, the above code speaks for itself. The “doCHAP()” function simply obtains the values of “user ID” and “password” fields that usually compose a regular login form, and applies basic validation on the data by displaying silly alerts through the “showError()” function. Then, as I described before, the challenge string is appended to the hashed password, and next the MD5 function is again applied to the combined strings. The resulting value of this operation is assigned to a “challenge” hidden field for being transmitted to the server. The following line of code performs those operations:

    chlng.value=MD5(MD5(psw.value)+'<?php echo $_SESSION['challenge']?>');

    Of course the code would be rather incomplete without showing the corresponding login form:

    <form method="post" action="chap.php">

    User ID <input type="text" name="userid" id="userid"/><br />

    Password <input type="password" name="passwd" id="passwd"/><br />

    <input type="hidden" name="challenge" id="challenge" />

    <input type="submit" name="login" value="Log In" />

    </form>

    Now hopefully you have a clear idea of how a simple CHAP login system can be implemented. Notice that whenever the form is submitted, the password is never transmitted in plain text to the server. Instead, a hashed value is sent out. Quite good, huh?

    Wrapping up

    That’s about it for now. Despite the easiness of the given example, I provided you with enough code to play with and start building your own CHAP system. However, there is a long road ahead of us. In the next part of the series, I’ll be showing the code that runs on the server, in order to carry out client authentication, as well as setting up the basis for implementing a more efficient login system. Meet you in the next part!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · The first part of this three-part tutorial introduces the implementation of CHAP...
       · Is there any way I can convert this to a Java Applet?
       · It all looks good so far. I'm a little curious to know what exactly has to happen...
       · Thank you for your comments on this article. You can read the follow-up articles...
       · I guess there's a way. I'm not very versed in Java, but I'm pretty sure this can be...
       · Hello everyone,I was using this chap login with some modifications but I ended up...
       · Hello Edu,Thank you for posting your comments on my article about the CHAP login...
       · I know CHAP based systems can easily stop "replay attacks", but if the user's...
       · Thank you for commenting on my article. Now, regarding your post, I’m afraid you’re ...
       · Thank you for the reply. But allowing the user to log in by sending the password in...
       · Thank you again for the comments. Yes, you’re correct, but that’s precisely the...
       · Well-written article. Thanks.It seems that you have the MD5 of the user's...
       · Thanks for the kind comments on my CHAP article. Well, actually it doesn’t matter...
       · first of all thanks for the tutorial im sure many will find it useful and its...
       · Hey Alex, thanks for the comments, but I guess you miss the point of the article,...
     

    JAVASCRIPT ARTICLES

    - Building Dynamic Shadows with JavaScript and...
    - Active Client Pages: Chrys`s Approach
    - The Script Approach to Active Client Pages: ...
    - Principles of Active Client Pages: the Scrip...
    - Active Client Pages: the Script Approach
    - Building an RTF-capable Form with the Ext JS...
    - Creating a Multi-Tabbed Online Form with the...
    - jQuery Overview
    - Constructing a Multi-Column Online Form with...
    - Grouping Field Sets on Dynamic Web Forms wit...
    - Building Dynamic Web Forms with the Ext JS F...
    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT