Question: Help Grinch to break the password ( Version II ) : Assuming that the Grinch has now discovered the function responsible for generating the final

Help Grinch to break the password (Version II): Assuming that the Grinch has now discovered
the function responsible for generating the final authenticating string within the app implementation,
here is the code fragment. This function accepts the users password and a random number as input,
producing the final authenticating string. Grinch has utilized a sniffer and acquired the random number,
which is "drexel", and the resulting authenticating string is "d88abf8d6473db08c9203f
8e4d7981db9ad7a66e5afab0e1fc9df01a437f32d7." Now, Grinch seeks assistance in
determining the users actual password. (The password consists of fewer than 8 digits and comprises
solely numbers.)
This is the code I am working with:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashCalculator {
public static String calculateCombinedHash(String random, String password){
// Combine the two strings
String combinedString = random + password;
try {
// Create a MessageDigest instance for SHA-256
MessageDigest digest = MessageDigest.getInstance("SHA-256");
// Get bytes of the combined string
byte[] combinedBytes = combinedString.getBytes();
// Calculate hash bytes
byte[] hashBytes = digest.digest(combinedBytes);
// Convert hash bytes to hexadecimal representation
StringBuilder hexString = new StringBuilder();
for (byte hashByte : hashBytes){
// Convert each byte to hexadecimal format
String hex = Integer.toHexString(0xff & hashByte);
if (hex.length()==1){
hexString.append('0');
}
hexString.append(hex);
}
// Return the hexadecimal representation of the hash
return hexString.toString();
} catch (NoSuchAlgorithmException e){
e.printStackTrace();
return null; // Return null if hashing algorithm is not found
}
}
public static void main(String[] args){
String random = "drexel";
String hash ="d88abf8d6473db08c9203f8e4d7981db9ad7a66e5afab0e1fc9df01a437f32d7";
for (int i =0; i <100000000; i++){
String password = String.format("%07d", i); // Password is less than 8 digits
String combinedHash = calculateCombinedHash(random, password);
if (combinedHash.equals(hash)){
System.out.println("Found the password: "+ password);
break;
}
if (i %1000000==0){
System.out.println("Checked "+ i +" passwords so far...");
}
}
}
}
It is not showing anything of the password, but is showing the count of passwords attempted.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!