Cracking password in less number of Trials using brute forcea
I was assigned a code by my professor and was told to improve the performance of it.
The code is below.
Code :
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!
Re: need help improving performance on my program
Hello xisstar, welcome to the Java Programming Forums.
I am a bit confused. Please help me understand.
Re: need help improving performance on my program
Hi xisstar,
I have a brute force example here. Hopefully this will help you:
Code :
public class Brute {
public static String password = "ash0";
char[] canUse = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
int maxlen = password.length();
public static void main(String[] args) {
Brute b = new Brute();
}
public Brute() {
int k = 0;
while (k < canUse.length) {
nextString(new Character(canUse[k]).toString());
k++;
}
}
private void nextString(String s) {
int i = 0;
System.out.println(s);
while (i < canUse.length) {
String guess = s + new Character(canUse[i]).toString().trim();
if(guess.equals(password)){
System.out.println("PASSWORD CRACKED! Password = " + guess);
System.exit(0);
}
if (new String(s + new Character(canUse[i]).toString()).length() <= maxlen) {
nextString(s + new Character(canUse[i]).toString());
}
i++;
}
}
}