Question
please modify the code below to meet all the requirements also shown below. I have a protected5.zip file along with the zip4j library shown below.
please modify the code below to meet all the requirements also shown below. I have a protected5.zip file along with the zip4j library shown below.
import java.util.Calendar;
import net.lingala.zip4j.core.*;
import net.lingala.zip4j.exception.*;
public class PasswordCracker {
public static void main(String[] args) {
long startTime = System.currentTimeMillis(); // get the start time
char[] pwd = new char[3]; // char array to hold the password
crackPassword(pwd, 0); // call the recursive function
long endTime = System.currentTimeMillis(); // get the end time
System.out.println("Password cracked in " + (endTime - startTime) + "ms");
}
// This is a recursive function that finds the correct password.
public static void crackPassword(char[] pwd, int index) {
for (char c = 'a'; c
pwd[index] = c; // set the current index to the current letter
if (index == pwd.length-1) { // if we have reached the end of the array
String password = new String(pwd); // get the String representation of the array
try {
ZipFile zipFile = new ZipFile("protected5.zip");
zipFile.setPassword(password);
zipFile.extractAll("contents");
System.out.println("Password found: " + password);
return; // exit the function
} catch (ZipException ze) {
// do nothing, this just means the password was incorrect
} catch (Exception e) {
e.printStackTrace();
}
} else { // if we have not reached the end of the array
crackPassword(pwd, index + 1); // recursively call the function
}
}
}
}
You have also been provided with another file, called protected5 . zip. As the name implies, this file is protected by a five-character password, still only made of lowercase letters. Because this password has more characters, your approach from Part 1 will take a long time to finish. Therefore, for this step you need to multithread your original implementation. This is required - you will not receive credit for programs that break this password using only a single thread. Your program must include a variable called numThreads that contains a hard-coded integer value to control the number of child threads spawned to look for the password. You can tweak this setting to your liking while testing your program - just make sure it is named as requested so that we can easily do the same. The multi-threaded version of your program should still quit as soon as any of the threads finds the password. To accomplish this, you will need a way to enable the main thread to detect when any of its child threads successfully finds the password and, if/when that happens, terminate the remaining child threads and quit. This is a great place to use a volatile static variable. When timing the amount of time the program takes, include both the time to generate the passwords and the time for the threads to run. Run your program with three and then with four threads and put the number of milliseconds it took to get the password as a comment at the top of your driver class (i.e. the one with main in it).Important: Because all of your threads will be trying to decrypt the file, if they were to all use the same file, that would create a bottleneck. To avoid this, each of your threads needs to make its own copy of the file to work on. Also, the method extractAll () , in addition to testing the password you set using set Password ( ) , leaves behind a directory (called contents in the starter code) that contains the results of its attempt at decrypting the . zip file. Since now all of the child threads will be using the extractAll ( ) method, you'll need a way to specify a different place for each one to write its results to by changing 'contents" to something unique for each thread like "contents-0", "contents-1", etc. Your threads should delete their copy of the target file and their contents directory when they finish. To receive full credit, this must all be done programmatically - you cannot manually make copies of the protected file before your program runs. As a reminder, here is some code that copies / deletes files: import java . nio. file. Files; import java . nio. file. Path; Files . copy (Path. of (srcFilename) , Path. of (destinationFilename) ) ; Files . delete (Path . of (filename) ) ; Keep in mind that to delete a directory, you must first delete any files inside it. Examples > java -cp . :zip4j-1.3.2. jar ZipCrackerSingleThread Password isStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started