Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please assist. I have included command lines that should replace certain lines, but I am not sure where they would go. Please insert codes in

Please assist. I have included command lines that should replace certain lines, but I am not sure where they would go. Please insert codes in proper places. When code compiles it should look like snapshot below. I am not certain to to code dictionary file. It should generate automatically if code is written correctly I suppose.

As per requirement, the password file and hash value should be asked from the user.

i.e. the code in the main function should look like. (instead of command line args)

public static void main(String[] args) {

Scanner scan = new Scanner(System.in); //import java.util.Scanner (write the statement on top of the file.

// read password file path from the user.

System.out.println("Enter the path of password File: ");

String dictionaryFile = scan.next();

System.out.println("Enter the Hash of Password: ");

long saltedPasswordHashValue = scan.nextLong(); // In requirement asked to store in LONG data type .

Commented out the lines:

//System.out.println(" Password recovery by ");

//if (args.length != 2){ // System.out.printf("Invalid number of arguments. Please provide valid number of argumets."); // System.out.println("Usage: RecoverPassword "); //}

//String dictionaryFile = args[0]; //String saltedPasswordHashValue = args[1];

===============================================================

Then the comparision of the user entered hash value and hash value calculated should be....

if(Long.toString(saltedPasswordHashValue).compareTo(hashString)==0){ // as we have stored the input hash to long

==================================================================

Here also we have to use LONG instead of int data type:

public String getHashForPassword(String saltedPassword){ long hash; // Changed to Long String hashString; String saltLeft = saltedPassword.substring(0,7); String saltRight = saltedPassword.substring(7,saltedPassword.length());

// changed to Long hash = ((243 * Long.parseLong(saltLeft)) + Long.parseLong(saltRight)) % 85767489; hashString = Long.toString(hash);

return hashString; }

image text in transcribed

Code to compile.

import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;

public class RecoverPassword { /** * * @param source for which ascii equivalent need to be generated * @return ascii equivalent of source */ public String generateASCII(String source){ String charASCII = ""; for (char c:source.toCharArray()){ charASCII += Integer.toString((int)c); } return charASCII; } /** * * @param saltedPassword for which hash value to be generated * @return hash string for corresponding salted password */ public String getHashForPassword(String saltedPassword){ int hash; String hashString; String saltLeft = saltedPassword.substring(0,7); String saltRight = saltedPassword.substring(7,saltedPassword.length());

hash = ((243 * Integer.parseInt(saltLeft)) + Integer.parseInt(saltRight)) % 85767489; hashString = Integer.toString(hash);

return hashString; }

public static void main(String[] args) {

System.out.println(" Password recovery by ");

if (args.length != 2){ System.out.printf("Invalid number of arguments. Please provide valid number of argumets."); System.out.println("Usage: RecoverPassword "); }

String dictionaryFile = args[0]; String saltedPasswordHashValue = args[1]; String recoveredPass=null, recoveredSalt=null, recoveredPassASCII=null; boolean passFound = false; int index = 1; List candidatePasswords = new ArrayList(); Map passwordToASCII = new HashMap();

System.out.println("\tDictionary file name " + dictionaryFile); System.out.println("\tSalted password hash value " + saltedPasswordHashValue);

/* Read file content*/

try { BufferedReader br = new BufferedReader(new FileReader(new File(dictionaryFile))); String line; while ((line = br.readLine()) != null) { if(line.trim() != "") { candidatePasswords.add(line);

} } } catch (FileNotFoundException e) { System.err.println("Dictionary file not found."); } catch (IOException e) { System.err.println("Problem while reading dictionary file."); }

RecoverPassword recoverPassword = new RecoverPassword(); String salt, hashString, saltedPassword, passASCII; int numberOfCombinations = 0;

/* Print index */

System.out.println("Index Word Unsalted ASCII equivalent"); for(String pass:candidatePasswords){ passASCII = recoverPassword.generateASCII(pass); passwordToASCII.put(pass, passASCII);

System.out.println(" " + Integer.toString(index) + " : " + pass + " " + passwordToASCII.get(pass));

index++; }

/* Recover password */ for(String pass:passwordToASCII.keySet()){ /* Salt password */ passASCII = passwordToASCII.get(pass); for(int i=0; i

hashString = recoverPassword.getHashForPassword(saltedPassword);

numberOfCombinations++;

if(saltedPasswordHashValue.compareTo(hashString)==0){

passFound = true;

recoveredPass = pass; recoveredPassASCII = passASCII; recoveredSalt = salt;

break; } } }

if(passFound){ System.out.println("Password recovered:"); System.out.println("Password recovered : " + recoveredPass); System.out.println("ASCII value : " + recoveredPassASCII); System.out.println("Salt value : " + recoveredSalt); }else { System.out.println("Password not found in dictionary."); }

System.out.println("Combination tested: " + Integer.toString(numberOfCombinations));

} }

yu787926enet1547:-$ javac Saltpassword.java yu787926enet1547:- java Saltpassword Please enter the name of the password file: password.txt Please enter the target hash value: 17215323 assword was recovered assword: ZFXOML Salt vaule: 963 r of entries searched: 99963 you want to try again (y): lease enter the target hash value: t 1234 sword was not foundI r of entries searched: 10080 0o you want to try again (y)a

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2016 Riva Del Garda Italy September 19 23 2016 Proceedings Part 3 Lnai 9853

Authors: Bettina Berendt ,Bjorn Bringmann ,Elisa Fromont ,Gemma Garriga ,Pauli Miettinen ,Nikolaj Tatti ,Volker Tresp

1st Edition

3319461303, 978-3319461304

More Books

Students also viewed these Databases questions

Question

4. Describe cultural differences that influence perception

Answered: 1 week ago