0

okay, so I have been working on a java brute force program that will read passwords from a txt file and stop when it matches a password stored in a String variable. For, some reason my code goes on reading the whole txt file and does not stop even if it matches the correct password.

Here's my code.

PasswordCracker2.java

    import java.io.*;
public class PasswordCracker2 {
    public static void main(String[] args) throws IOException {
        String password = "busch@123";
        File f = new File("wordlist.txt");
        BufferedReader b = new BufferedReader(new FileReader(f));

        String readLine = "";

        System.out.println("[+] Trying Passwords from wordlist \n");
        String guess_password;

        while ((guess_password = b.readLine()) != null) {
            System.out.println("[+] Password Attempt:  " + guess_password);         
            if(guess_password == password) {
                System.out.println("[+] Password Cracked: " + guess_password);
            }
        }
        }
    }
  • Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Jul 21 '18 at 14:02
  • When dealing with string comparison, you must use `text1.equals(text2)`. Also, once you find the match, you need to break out of the while loop. ` while ((guess_password = b.readLine()) != null) { System.out.println("[+] Password Attempt: " + guess_password); if(guess_password.equals(password)) { System.out.println("[+] Password Cracked: " + guess_password); break; } }` – M. Al Jumaily Jul 21 '18 at 14:05

0 Answers0