Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search a text file and exit once the correct entry is found.

Status
Not open for further replies.

systemsadmin3000

Technical User
Sep 8, 2005
65
TT
I have a peice of code that takes a 4 character word and parses it through a one-way function and encrypts it. It checks the word against the password file and returns the encrypted password. I have to modify the code in that instead of accepting random entries, it would instead seach a dictionary file and retuns that correct password once it exists in that file. I am new to Java and would appreciate any assitance. Below is a copy of the sourse code:

/* A program which simulates a simple username/password log-on system.*/
import java.io.*;
import java.util.*;
import java.text.DateFormat;
public class TESTSEARCH
{
/*Applies the fast modular exponentiation method of the subject guide*/
public static long fastmodexp(long g, long a, long p)
{
long y=1;
long u=g%p;
do
{
if(a%2==1) y=(y*u)%p;
a=(int)Math.floor(a/2);
u=(u*u)%p;
}while(a!=0);
return y;
}
/*Converts each string into a unique number which is the input for the one-way function*/
static long associate(String t)
{
long num=0;
char[] chars = t.toCharArray();
for (int i=0;i<t.length();i++){
num=(num*100)+((int)((chars%100)));}
return num;
}
/*Encrypts the password by first converting it into an integer value num
and then using the one-way function 2^n mod 405145267 */
static long Encrypt(String p) throws IOException
{
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
long prime = 405145267;
long g = 2;
long num=associate(p);
//applies the one-way function to encrypt the password
long encrypt_word = fastmodexp(g,num,prime);
System.out.println(encrypt_word);
return(encrypt_word);
}
/*Checks input passwords with passwords in file*/
static boolean check_password(String t) throws IOException
{
int[] pswdfile;
int no_of_passwords=6;
boolean found=false;
//This is the password file
pswdfile=new int[no_of_passwords];
pswdfile[0]=249316753;
pswdfile[1]=3809948;
pswdfile[2]=240781514;
pswdfile[3]=14751869;
pswdfile[4]=17904244;
pswdfile[5]=316986290;
long encrypted_t=Encrypt(t);
for(int i=0;i<no_of_passwords;i++)
{
if(encrypted_t==pswdfile)
{
System.out.println(t+" is the password of user number "+(i+1));
System.out.println("Search successful at: "+getCurrentTimestamp());
found=true;
}
}
return found;
}
/*returns the current time and date*/
public static String getCurrentTimestamp()
{
//get current locale settings
DateFormat df = DateFormat.getDateTimeInstance();
//sets dNow to the current date and time
Date dNow = new Date();
return df.format(dNow);
}
/*This is the main program.*/
public static void main(String[] args) throws IOException
{
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
String password="";
do{
System.out.print("Enter your password: ");
password=in.readLine();
//continue looping until a correct password is entered
}while(check_password(password)!=true);
 
Please use code-tags and indentation
Code:
/* A program which simulates a simple username/password log-on system.*/
import java.io.*;
import java.util.*;
import java.text.DateFormat;

public class TESTSEARCH
{
	/* Applies the fast modular exponentiation method of the subject guide */
	public static long fastmodexp (long g, long a, long p)
	{
		long y = 1;
		long u = g % p;
		do
		{
			if (a % 2 == 1) y = (y * u) % p;
			a = (int) Math.floor (a / 2);
			u = (u * u) % p;
		} while (a != 0);
		return y;
	}

	/* Converts each string into a unique number which is the input for the one-way function */
	static long associate (String t)
	{
		long num = 0;
		char[] chars = t.toCharArray ();
		for (int i = 0; i < t.length (); i++)
		{
			num = (num * 100) + ((int) ( (chars[i] % 100)));
		}
		return num;
	}

	/*
	 * Encrypts the password by first converting it into an integer value num and then using the one-way function 2^n mod 405145267
	 */
	static long Encrypt (String p) throws IOException
	{
		BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
		long prime = 405145267;
		long g = 2;
		long num = associate (p);
		// applies the one-way function to encrypt the password
		long encrypt_word = fastmodexp (g, num, prime);
		System.out.println (encrypt_word);
		return (encrypt_word);
	}

	/* Checks input passwords with passwords in file */
	static boolean check_password (String t) throws IOException
	{
		int[] pswdfile;
		int no_of_passwords = 6;
		boolean found = false;
		// This is the password file
		pswdfile = new int[no_of_passwords];
		pswdfile[0] = 249316753;
		pswdfile[1] = 3809948;
		pswdfile[2] = 240781514;
		pswdfile[3] = 14751869;
		pswdfile[4] = 17904244;
		pswdfile[5] = 316986290;
		long encrypted_t = Encrypt (t);
		for (int i = 0; i < no_of_passwords; i++)
		{
			if (encrypted_t == pswdfile[i])
			{
				System.out.println (t + " is the password of user number " + (i + 1));
				System.out.println ("Search successful at: " + getCurrentTimestamp ());
				found = true;
			}
		}
		return found;
	}

	/* returns the current time and date */
	public static String getCurrentTimestamp ()
	{
		// get current locale settings
		DateFormat df = DateFormat.getDateTimeInstance ();
		// sets dNow to the current date and time
		Date dNow = new Date ();
		return df.format (dNow);
	}

	/* This is the main program. */
	public static void main (String[] args) throws IOException
	{
		BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
		String password = "";
		do
		{
			System.out.print ("Enter your password: ");
			password = in.readLine ();
			// continue looping until a correct password is entered
		} while (check_password (password) != true);
	}
}

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top