Subscribe to A Kid's scribblings        RSS Feed
-----

Password enabled basic database

Icon 1 Comments
After thinking a lot,came up with a basic password enabled database,and which doesn't display the passwords being typed. A database with basic login facilities, and register facilities for new users.

I kept it rather simple, for better understanding, Without JDBC, Without GUI. So let come to point, and stop blabbering uneccesarily about this thing.

And before i start describing it,another word of caution,don't try to run this in an IDE,then this might throw NullPointerException, as i have used the java.io.Console class to take I/O purposes. As IDEs doesn't have arrangements to handle java.io.Console class calls. So folks open up the old faithfull command prompt and execute this program in the old traditional approach. Here i have used this particular class for I/O purposes, so that when the user types the password it is not echoed back on the screen. This facility is offered only by this class in java.io package this is the only class where this facility is offered. Next comes the database part,for which i have used java.util.Properties class to create the so called password protected database is created. This class uses hashing technique to store values , i.e indexes are replaced by unique keys of user's choice, & for every unique key there is a value stored(null can also be stored). Now to access that particular value we have to refer to it by the unique key, or rather reference it using the key. So in the sense it creates some sort of password protection stuff. Now here goes the source code.



import java.io.*;
import java.util.*;
public class Password 
{
private static String path="C:\\Users\\Souvik\\Documents\\NetBeansProjects\\goja\\src\\gojabe\\g_db.DAT";
private static Properties db= new Properties();
    public static void main (String args[])throws Exception 
    {
       Console c = System.console();
        if (c == null) {
            System.err.println("No console.");
            System.exit(1);
        }
        char [] newPassword1;char [] newPassword2;char [] oldPassword;
        String login,phoneNo,email_id;
        boolean noMatch;
        System.out.println("Press 'l' to login");
        System.out.println("Press 'r' to register");
        String ch=c.readLine("Enter your choice");
        if(ch.equals("l"))
        {
        login =c.readLine("Username: ");
        oldPassword =c.readPassword("Password: ");
        if (verify(login, oldPassword)) {
            noMatch=true;
            System.out.println("Press 'p' to change password");
            System.out.println("Press 'i' to update info");
            System.out.println("Press 'v' to view info");
            String choice=c.readLine("Enter your choice");
            if(choice.equals("p"))
            do {
                newPassword1=c.readPassword("Enter your new password: ");
                newPassword2=c.readPassword("Enter new password again: ");
                noMatch = ! Arrays.equals(newPassword1, newPassword2);
                if (noMatch) {
                    System.out.println("Passwords don't match. Try again.\n");
                } else {
                    change(login, newPassword1,oldPassword);
                    System.out.println("Password for "+login+" changed");
                }
            } while (noMatch);
            else if(choice.equals("i"))
            {
                login =c.readLine("Update your login id: ");
                phoneNo =c.readLine("Update your telephone no. : ");
                email_id =c.readLine("Update your email id: ");
                update(login,phoneNo,email_id,oldPassword);
            }
            else if(choice.equals("v"))
                view(login,oldPassword);
            else
                System.out.println("Wrong choice");
        }
        else
            System.out.println("\nLogin Failed....");
            }
        else if(ch.equals("r"))
        {
        login =c.readLine("Enter your name: ");
        phoneNo =c.readLine("Enter your telephone no. : ");
        email_id =c.readLine("Enter your email id: ");
        do {
                newPassword1=c.readPassword("Enter your password: ");
                newPassword2=c.readPassword("Enter your password again: ");
                noMatch = ! Arrays.equals(newPassword1, newPassword2);
                if (noMatch)
                    System.out.println("Passwords don't match. Try again.\n");
                if(passfound(newPassword1))
                {
                    System.out.println("\nPassword is too weak\nTry something else\n");
                    continue;
                }
            } while (noMatch);
        update(login,phoneNo,email_id,newPassword1);
        }
        else
            System.out.println("\nInvalid choice!!!");
    }    

    /*Function to verify the authentication of a particular user's profile*/

    private static boolean verify(String login,char[] password)throws Exception
    {
        boolean flag=false;
        String pass=new String(password);
        FileInputStream fis=new FileInputStream(path);
        if(fis!=null)
            db.load(fis);
            String profile[]=((String)db.get(pass)).split(" ");
        if(profile[0].equals(login))
            flag=true;
        fis.close();
        return flag;
    }

    /*Function to change the old password for a particular user with a new password*/

    private static void change(String login,char[] newPassword,char[] oldPassword )throws Exception 
    {
       FileInputStream fis=new FileInputStream(path);
       FileOutputStream fos=new FileOutputStream(path,true);
       String np=new String(newPassword);
       String op=new String(oldPassword);
       if(fis!=null)
            db.load(fis);
       String remove=(String)db.remove(op);
       String put = (String)db.put(np,remove);
       db.store(fos,"Contacts");
       fis.close();
       fos.close();
    }

    /*Function change the info of a particular user*/

    private static void update(String login,String phoneNo,String email_id,char[] password)throws Exception
    {
       FileInputStream fis=new FileInputStream(path);
       FileOutputStream fos=new FileOutputStream(path,true);
       String pass=new String(password);
       if(fis!=null)
            db.load(fis);
       String profile=login+" "+phoneNo+" "+email_id;
       String put = (String)db.put(pass,profile);
       db.store(fos,"Contacts");
       fis.close();
       fos.close();
    }

    /*Function to view the user details of particular user*/

    private static void view(String login,char[] password)throws Exception
    {
        FileInputStream fis=new FileInputStream(path);
        String pass=new String(password);
        if(fis!=null)
            db.load(fis);
        String profile[]=((String)db.get(pass)).split(" ");
        System.out.println("User Profile\n");
        System.out.println("Username ::"+profile[0]+"\n");
        System.out.println("Phone No ::"+profile[1]+"\n");
        System.out.println("email id ::"+profile[2]+"\n");
        fis.close();
    }

    /*Function to check wheather the had been earlier used to store something in the database*/

    private static boolean passfound(char[] password)throws Exception
    {
        boolean flag=true;
        FileInputStream fis=new FileInputStream(path);
        String pass=new String(password);
        if(fis!=null)
            db.load(fis);
        flag=db.containsKey(pass);
        return flag;
    }
}

1 Comments On This Entry

Page 1 of 1

MaxxGorod Icon

02 March 2012 - 06:27 AM
Hmmm...... nice but your coding style is archaic... try and organize your design... and implement a methodical approach else Peter Griffin would be upset
0
Page 1 of 1

Recent Comments

April 2014

S M T W T F S
  12345
6789101112
13141516171819
20 212223242526
27282930   

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)