Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Cracking password in less number of Trials using brute forcea

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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!
    Last edited by Deep_4; November 7th, 2012 at 11:26 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: need help improving performance on my program

    Hi xisstar,

    I have a brute force example here. Hopefully this will help you:

    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++;
            }
     
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Replies: 1
    Last Post: May 13th, 2008, 08:08 AM

Tags for this Thread