Click here to Skip to main content
Click here to Skip to main content

Virtual keyboard - Secure keying of password

, 12 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Virtual keyboard - can be used for secure keying of passwords.

Introduction

Secure login for bank sites also provide a virtual keyboard to enter the password in a public system. This project provides an implementation for a set pattern.

virtualKeyboard.JPG

Using the code

The requirements for such a keyboard would be:

  1. Show all keys which can be used for the password field. This would include alphabets, numbers, and special characters.
  2. On click of each key, convert all keys to # so that somebody looking over the shoulder will not be able to figure it out.
  3. Provide Caps Lock, backspace, and Clear buttons.

Since all keys are similar in function apart from the value, we can use direct HTML and a template to create the bulk of the keyset.

<input type="button" value="a" id="btn1"/>

This would give us a button, however we want more. Hence we add the following events:

  • onmousedown - change it to #
  • onmouseup - restore key value
  • onclick - the complete action of clicking - write the character to the password box

This can be held by a variable for ease of use later on:

string buttonHtmlString = "<input type=\"button\" class=\"keys\" 
  value=\"{0}\" name =\"{1}\" id=\"btnVirKey\" 
  onclick=\"func_click(this.value);\" onmousedown = \"hashthekeys();\" 
  onmouseout=\"unhashthekeys();\" onmouseup=\"unhashthekeys();\" />";

Another aspect is, we are using up some screen area for displaying this, and hence we have to program for a fixed pattern of keys.

I have selected a format where the keys look like:

/*      row1 - 15 char - alphabets
 *      row2 - 11 char - alphabets
 *      row3 - 10 char - numbers + 2 char symbols
 *      row4 - 15 char - symbols
 *      row5 - 15 char - symbols
 *      row6 - Back Space, Clear, Caps Lock
 */

Hence, I am going to set a specific pattern to fit my area and randomize to that extent. You will have to find out how many keys fit into the screen area and then decide on it. I decided to keep the width of my table to 300px, and on trial error basis, I have a max of 15 characters per line.

Generating the keys

I have created three lists, for each set of different characters I need.

List<string> listAlphabets = new List<string>();
List<string> listNumbers = new List<string>();
List<string> listSymbols = new List<string>();

I fill them up, programmatically or manually, as appeals to me.

private void CreateAlphabets()
{
    for (int i =97; i <= 122; i++)
    {
        //a = 97; z=122
        listAlphabets.Add(char.ConvertFromUtf32(i).ToString());
    }
}

private void CreateNumberlist()
{
    for (int i = 0; i < 10; i++)
    {
        listNumbers.Add(i.ToString());
    }
}

private void CreateSymbolList()
{       
    string[] symbollist = new string[] {"!","@","#","$","%","^","&",
      "*","(",")","_","-","+","=","~","`",""",":",";","'",
      ",",".","/","<",">","?","{","}","[","]","|","\\"};
    listSymbols.AddRange(symbollist);
}

Initially, adding all keys to a list and then removing them on insertion will do the trick.

I add a row after the fifteenth button to fit with the pattern. The same goes with the other sets - numbers, symbols.

//Filling first 2 rows -Alphabets
for (int i = 0; i < 26; i++)
{
    int next = rand.Next(1,listAlphabets.Count) -1;
    htmltbl.Append(string.Format(buttonHtmlString, 
                   listAlphabets[next], listAlphabets[next]));
    listAlphabets.RemoveAt(next);
    if(i== 14)
    {
        //adding the second row
        htmltbl.Append("</td></tr><tr><td>");
    }
}

For the JavaScript, we have to declare a form and have a common button name to enable ease of looping to set and reset the values. We need a swap field for storing the actual value - name can be used for this.

We just append the value when the onclick event triggers:

function func_click(charVal) {   
    finalresult = document.getElementById("txtoutput").value;
    document.getElementById("txtoutput").value = finalresult + charVal;
}

The hashing and unhashing - since all the button names are the same, FormName.ButtonName[index] is the easiest way to set and reset the button values.

function hashthekeys() {
 for (var z = 0; z < document.formVitualKeyboard.btnVirKey.length; z++) {
            document.formVitualKeyboard.btnVirKey[z].value = '#';
        }
 for (var z = 0; z < document.formVitualKeyboard.button2.length; z++) {
    document.formVitualKeyboard.button2[z].value = '#';
        }
}
    
function unhashthekeys() {
 for (var z = 0; z < document.formVitualKeyboard.btnVirKey.length; z++) {
    document.formVitualKeyboard.btnVirKey[z].value = 
             document.formVitualKeyboard.btnVirKey[z].name;
 }
 for (var z = 0; z < document.formVitualKeyboard.button2.length; z++) {
    document.formVitualKeyboard.button2[z].value = 
             document.formVitualKeyboard.button2[z].name;
 }
}

Backspace, Clear, and Caps Lock have custom functions, and the buttons are also defined separately.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

Comments and Discussions

 
QuestionHelp!! PinmemberGeorgeHLord18-Oct-13 20:40 
GeneralMy vote of 5 Pinmemberjawed.ace13-Dec-11 0:00 
GeneralMy vote of 5 PinmemberMember 825477112-Dec-11 20:17 
GeneralVery good example. Useful. Pinmembersudhansu_k12312-Dec-11 19:19 
GeneralMy vote of 3 [modified] Pinmembersudhansu_k12312-Dec-11 19:18 
Nice.
GauriShankar[^]

modified 27-Dec-11 4:49am.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141030.1 | Last Updated 12 Dec 2011
Article Copyright 2011 by DestinyCoder007
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid