jeudi 8 octobre 2020

Brute force gone wrong

i really need your help. i am trying to make a small program in java that generates a random 4 digit number. so from 0000 to 9999. and then the program should try to brute force and guess the random generated number by trying every 4 digit number option. so far this is where i am at with my code.

public static void main(String[] args) {
        
        Random r = new Random();
    
        String password = String.format("%04d", r.nextInt(10001));
        String guessPassword;
        int realPassword = 0;
        String realP = String.valueOf(realPassword);
        
        while(rP!= password){
            guessPassword = String.format("%04d", r.nextInt(10001));
            System.out.println(guessPassword);
            
            if(guessPassword == password){
             rP = guessPassword;
                System.out.println("The password is " + realP);
            }
            
            else{
                continue;
            }
        }
    }
}

i am quite sure that the problem lies with the line

guessPassword = String.format("%04d", r.nextInt(10001)); 

because as far as i can see it just keeps guess numbers without finding the correct one. but i cant seem to find another way of doing it. but i might be wrong.

is there anyone out there that my be able to tell me where and why i am wrong?




Aucun commentaire:

Enregistrer un commentaire