systemsadmin3000
Technical User
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);
/* 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);