Thursday, February 4, 2010

Network Security Experiment 1

/*
Following is a Java program to check strength of password entered by user utilizing following parameters
a)Combination of alphabets with digits & special symbols
b)Length of password
c)Repetition of characters in password


This program uses HashTable to store frequency of characters.
*/

import java.io.*;
import java.util.Hashtable;
import java.util.Enumeration;



/**
 * @author Pawan Singh
 *
 */

public class passwordStrength {


    public static void main(String[] args) throws IOException {


        System.out.println("Enter password: ");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String password=br.readLine();
        int strength=passwordStrength.checkStrength(password); 
        // checkStrength static method called
        System.out.println("Strength is "+strength+" out of 100");


        if (strength<0)
            System.out.println("Very Weak Password");
        else if (strength<34)
            System.out.println("Weak Password");
        else if(strength<68)
            System.out.println("Good Password");
        else
            System.out.println("Strong Password");


    }
   
   


    static public int checkStrength(String password)
    {
        int strength;
        //Initially parameter evaluation
        int len =password.length(),no_of_digits=0,no_of_alpha=0,no_of_spl=0;
        Hashtable freq = new Hashtable ();
        char c;
        Integer t;


        for(int i=0;i
        {
            //Evaluating number of types of characters
            c=password.charAt(i);
            if (Character.isDigit(c))
                no_of_digits++;
            else if (Character.isLetter(c))
                no_of_alpha++;
            else
                no_of_spl++;

            if (!freq.containsKey(c))
                freq.put(c, 1);
            else
            {
                t=freq.get(c);
                freq.remove(c);
                freq.put(c,t+1);
            }
            //End of for loop
        }


        //Finally Evaluation of strength
        strength=len*4;
        Enumeration e=freq.keys(); 
        while(e.hasMoreElements())
        {
            c=(Character)e.nextElement();   
            if( freq.get(c)>1)
            {
                strength-=5*(Integer)freq.get(c);//Reduction on behalf of repetition
            }
            //End of loop   
        }

        if (no_of_digits>2)
            strength+=5;
        if(no_of_spl>2)
            strength+=10;
        if(no_of_digits>0 && no_of_alpha>0)
            strength+=15;
        if(no_of_digits>0 && no_of_spl>0)
            strength+=15;
        if(no_of_spl>0 && no_of_alpha>0)
            strength+=15;
        if(no_of_alpha==0 && no_of_spl==0)
            strength-=15;
        if(no_of_digits==0 && no_of_spl==0)
            strength-=20;
        if(strength>100)
            strength=100;

        return strength;
    }   





}