I was assigned a code by my professor and was told to improve the performance of it.
The code is below.
final String GOOD_PASSWORD = "goodmorning"; String enteredPassword = ""; boolean passwordOk = false; long numOfTrials = 0; Random generator = new Random ( ); int len; final int PASSWORD_LEN_LIMIT = 30; int charCode; boolean charOk; do { enteredPassword = ""; //guess the number of chars in the password 1 - 30 chars len = generator.nextInt (PASSWORD_LEN_LIMIT) + 1; for (int i = 0; i < len; i++) { //creating a number that can be used as a char in a password //digits: 48 - 57 //upper case: 65 - 90 //lower case: 97 - 122 charOk = false; do { charCode = generator.nextInt (75) + 48; if (((charCode <= 57) && (charCode >= 48)) || ((charCode <= 90) && (charCode >= 65)) || ((charCode <= 122) && (charCode >= 97))) charOk = true; } while (!charOk); enteredPassword = enteredPassword + (char) charCode; } System.out.print (enteredPassword + "///"); JOptionPane.showMessageDialog (null, enteredPassword); if (enteredPassword.equals (GOOD_PASSWORD)) passwordOk = true; else numOfTrials++; } while (!passwordOk); JOptionPane.showMessageDialog (null, "password cracked after " + numOfTrials + " trials"); System.exit (0); } }
What the program is set to do is have the computer guess the characters of the password and then have it randomly generate letters and digits in an attempt to guess what the password might be. I tried making the good password less letters (would be like having a website require a smaller amount of maximum characters in a password) and still have to go through an immense amount of trials to get it to work. Do you guys have any ideas on how I might be able to get the password to be "cracked" in a fewer amount of tries? Possibly a brute force code or something, and if so how would that be implemented.
Thanks!