Scarl Scarl - 1 month ago 7
Java Question

Specifying while condition causes program to return nothing

I'm currently working on a brute-force password cracking method because I want to try it out and do something new. I'm providing the methods I'm working on below but here's first what I'm trying to do. Initially, I'm generating the hash of a possible value for the password and comparing it against the one I have in my "sample.txt" file that contains a list of hashed passwords. So basically my goal is to read the hashed password value from that external file and compare it against all possible 3/4 hashed password values.

When I ran the program, I accidentally kept the condition of the while statement in the "BruteForce" method to true and it endlessly ran, however when I set the condition to keeping the password value generated to be having a length of 3 and 4 only, the program suddenly terminates I don't know why, I've tried debugging the program to see where things go wrong but i was not able to deduce anything.

Here's what's in my main method and the "bruteforce" method :

public TestClass(char[] characterSet, int guessLength) {
cs = characterSet;
cg = new char[guessLength];
Arrays.fill(cg, cs[0]);
}

public static void bruteForce(String username, String hashed_pw) {

//username is the username from the input
//hashed_pw is the hashed value of password
String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
char[] charset = chars.toCharArray();

TestClass bf = new TestClass(charset, 1); //random generation of possible pw value

String attempt = bf.toString();

while ((attempt.length() == 3) || (attempt.length() == 4)) {

String hashed_input = doHash(attempt); //hash the possible pw value

System.out.println("");
System.out.println("Attempt result is: " + attempt);
System.out.println("Hashed of attempt: " + hashed_input);
System.out.println("Hashed Password is : " + hashed_pw);
System.out.println("");

if (hashed_input.equals(hashed_pw)) {
System.out.println("Password Found: " + attempt);
System.out.println(username + "'s password is: " + attempt);
break;
} else {
attempt = bf.toString();
bf.increment();
}
// attempt = bf.toString();
// System.out.println("" + attempt);
// bf.increment();
// return attempt;

}
// return attempt;
}

public char[] cs;
public char[] cg;


public void increment() {
int index = cg.length - 1;
while (index >= 0) {
if (cg[index] == cs[cs.length - 1]) {
if (index == 0) {
cg = new char[cg.length + 1];
Arrays.fill(cg, cs[0]);
break;
} else {
cg[index] = cs[0];
index--;
}
} else {
cg[index] = cs[Arrays.binarySearch(cs, cg[index]) + 1];
break;
}
}
}

@Override
public String toString() {
return String.valueOf(cg);
}


When running the code with bruteForce(s[0],s[1]) it does not provide any output but only gives the BUILD SUCCESSFUL message.

s[0] is the username of the user I'm trying to deduce their password
s[1] is the hashed password I read from an external file


I compare the s[1] value against the hashed_input value in the bruteForce method, with the condition that the possibly generated password of my user has a length of 3 or 4 characters only

Answer
TestClass bf = new TestClass(charset, 3);

Will solwe your problem.

Comments