Click here to Skip to main content
15,896,269 members
Articles / Web Development / HTML

JavaScript Password Protection and Session Management

Rate me:
Please Sign up or sign in to vote.
4.07/5 (11 votes)
17 Jul 200213 min read 154.3K   2.7K   46  
This client-side code implements secure password protection and user/session management using cookies based on rc4 encryption.
<html>
<head>
<script language="JavaScript" src="dvxcrypt.js"></script>
<script language="JavaScript">
function aborted()
{
  alert('Aborted!');
  return false;
}
function newuser()
{
  username = prompt('Enter user name:','');
  if(!username) return aborted();
  password = prompt('Enter password:','');
  if(!password) return aborted();
  var ngrp = 0;
  var more = false;
  var groups = new Array();
  var masters = new Array();
  do
  {
    groupname = prompt('Group #'+ngrp+' name:','Group'+ngrp);
    if(!groupname) break;
    groupmaster = prompt('Group #'+ngrp+' master password:','');
    if(!groupmaster) break;
    groups[ngrp] = groupname;
    masters[ngrp] = groupmaster;
    more = confirm('Add user to another group?');
    ngrp++;
  } while(more);
  if(ngrp == 0) return aborted();
  var s = "DeclareUser('"
  s += b64MD5(username);
  s += "','";
  s += b64MD5(password);
  s += "'";
  for(i = 0; i < ngrp; i++)
  {
    s += ",[";
    s += groups[i];
    s += ",'";
    s += textToBase64(rc4(password,masters[i]));
    s += "']";
  }
  s += ");";
  document.f.out.value = s;
  alert('User code created.\nCopy / Paste into your source document.');
  return true;
}
function newgroup()
{
  groupname = prompt('Group name:','MyGroup');
  if(!groupname) return aborted();
  alert('You will be prompted to enter a master password for the group.\nA master password is only used to add new users to the group, and to encrypt resources for this group.\nRemember this one carefully!')
  grouppass = prompt('Group master password:','');
  if(!grouppass) return aborted();
  var s = "var ";
  s += groupname;
  s += " = new Group('";
  s += b64MD5(grouppass);
  s += "');";
  document.f.out.value = s;
  alert('Group code created.\nCopy / Paste into your source document.');
  return true;
}
function newresource()
{
  resourcename = prompt('Resource name:','MyResource');
  if(!resourcename) return aborted();
  groupname = prompt('Which group should this resource belong to?','MyGroup');
  if(!groupname) return aborted();  
  masterpass = prompt('Enter the master password for the group:','');
  if(!masterpass) return aborted();  
  var s = "var ";
  s += resourcename;
  s += " = new Resource(";
  s += groupname;
  s += ",'";
  s += textToBase64(rc4(masterpass,document.f.resource.value));
  s += "');";
  document.f.resource.value = s;
  alert('Resource encrypted.\nCopy / Paste into your source document.');
  return true;
}
</script>
<style>
body { font-family: verdana,helvetica,arial; font-size: x-small; }
</style>
</head>
<body>
<form name=f>
Users &amp; Groups:<br>
<textarea name=out cols=70 rows=5></textarea><br>
<input type=button value="Generate Group Code" onclick="newgroup()">
<input type=button value="Generate User Code" onclick="newuser()">
<p><hr>
Resource content:<br>
<textarea name=resource cols=70 rows=15></textarea><br>
<input type=button value="Encrypt above resource" onclick="newresource()">
</form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Engineer Nokia
Denmark Denmark
My programming experience started a long time ago in
QBasic (on a 25MHz 486).
I'm now mainly using Java, C++, C, MFC, Perl and PHP, but have used quite a number of other languages as well for various projects.

Comments and Discussions