Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please assist to correct code for successful compiling. I have included a snapsh

ID: 3703840 • Letter: P

Question

Please assist to correct code for successful compiling. I have included a snapshot of what results should look like.

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 <your-name>");

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

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

        System.out.println(" Dictionary file name " + dictionaryFile);
        System.out.println(" Salted 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<=999; i++){
                salt = String.format("%03d", i);
                saltedPassword = salt + passASCII;

                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));

    }
}

This is what I get when I run:

mi275326@net1547:~$ javac RecoverPassword.java
mi275326@net1547:~$ java RecoverPassword
<> Password recovery by <your-name>
Invalid number of arguments. Please provide valid number of argumets.Usage: RecoverPassword <file> <hashvalue>
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at RecoverPassword.main(RecoverPassword.java:39)

Successful Results should look like this:

password.tt Saltpassword.dass Saltpassword java

Explanation / Answer

Please Run the program using below commands


mi275326@net1547:~$ javac RecoverPassword.java
mi275326@net1547:~$ java RecoverPassword password.txt 17215323


Thanks, let me know if there is any concern. Comment and I will help more to correct it..
First thing is to make it compile. We will go step by step if anything is needed more.