Solved

Java - Alternating Values like a Password Cracker.

Posted on 2004-09-21
21
268 Views
Last Modified: 2010-03-31
Hi All,

I need to create a java program that is going to alternate all possible values in the alphabet, and numeric values.

It is similar to what a password cracker does.

First it shows: a -> b -> c... and go on... ab -> ac ->...

Can you help me ?

Do you know where I can find this algorithm ? Examples ?

Thanks,
Root.
0
Comment
Question by:root_start
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 5
21 Comments
 
LVL 3

Expert Comment

by:nesnemis
ID: 12114977
This program does something like that:

public class Cracker
{
      private String ALPHABET[] = {"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"};
      private int LIMIT = 3;
      /**
       * Constructor for Cracker.
       */
      public Cracker()
      {
            super();
            for(int i=0; i<ALPHABET.length; i++)
                  cracker(1, ALPHABET[i]);
      }

      public static void main(String[] args)
      {
            new Cracker();
      }
      public void cracker(int index, String password)
      {
            System.out.println(password);
            if(index == LIMIT)
                  return;
            for(int i=0; i<ALPHABET.length; i++)
                  cracker(index+1, password+ALPHABET[i]);
      }
}
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12115248
Very nice, nesnemis.

For the author:
 >"all possible values in the alphabet, and numeric values."
To add numbers to the generated sequences, simply add the numbers (and any other special characters you want), to the ALPHABET array, like so:

private String ALPHABET[] = {"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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

And to edit the length of the maximum 'passwords', simply change the variable LIMIT from 3 to the max length you want.

Points to nesnemis  =)

The below code is easier to understand I think, however, isn't as good as the above (at all), because for every time you want to produce an extra character, you must add another for() statement:

private String ALPHABET[] = {"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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

public static void main ( String [] args ) {

    for ( int a = 0; a < ALPHABET.length; a++ ) {
        for ( int b = 0; b < ALPHABET.length; b++ ) {
            for ( int c = 0; c < ALPHABET.length; c++ ) {
                System.out.println( ALPHABET[a] + ALPHABET[b] + ALPHABET[c] );
            }
        }
    }

}

That obviously produces all the possible (length 3) sequences with the characters in the ALPHABET array. :)

Hope that helps a bit.
[r.D]
0
 
LVL 3

Author Comment

by:root_start
ID: 12117184
Hi again All,

I was checking the code, and if I put the limit equals to 3, it is going to put:
1. a
2. aa
3. aaa
Before number 3, it is going to start to alternate the values.

And what I want is the following:
1. a
2. b
3. c

And before it:
...
n. ab
n. ac
n. ad

Is it clear ?

Thanks again for all your help,
Root.
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 9

Expert Comment

by:DrWarezz
ID: 12123552
In that case, you COULD refer to my code:

    for ( int a = 0; a < ALPHABET.length; a++ ) {
        for ( int b = 0; b < ALPHABET.length; b++ ) {
            for ( int c = 0; c < ALPHABET.length; c++ ) {
                System.out.println( ALPHABET[a] + ALPHABET[b] + ALPHABET[c] );
            }
        }
    }

Which in fact outputs it in the order:
a
b
c
...
aa
ab
ac
...
aaa
aab
aac
...etc..

Or, better yet, I just conjured this up:

public class Cracker
{
     private String ALPHABET[] = { "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
     private int LIMIT = 3;
     /**
      * Constructor for Cracker.
      */
     public Cracker()
     {
          super();
          int LIMIT2 = LIMIT;
          for ( int a = 0; a < LIMIT2; a++ ) {
               LIMIT = a+1;
               for(int i=0; i<ALPHABET.length; i++)
                    cracker(1, ALPHABET[i]);
          }
     }

     public static void main(String[] args)
     {
          new Cracker();
     }
     public void cracker(int index, String password)
     {
          if ( password.length() == LIMIT )
               System.out.println(password);
          if(index == LIMIT)
               return;
          for(int i=0; i<ALPHABET.length; i++)
               cracker(index+1, password+ALPHABET[i]);
     }
}


Hope it helps! :)
[r.D]
0
 
LVL 3

Author Comment

by:root_start
ID: 12123652
It helped so much...

By the way, how can I return the "generated" value and then test it and then restart it again from where it stopped ?

Root.
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12123665
What do you mean exactly? I THINK I know what you're on about, but could you please give an example?

Ta,
[r.D]
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12123677
Do you mean, something like;

You start the password generator, then you stop it WHILE it's working. How can you then pass the last generated 'password' to the program to continue producing from there?

If so, just say yes, and I can make you some code :)

[r.D]
0
 
LVL 3

Author Comment

by:root_start
ID: 12123740
yes... it is exactly what I need.

I need to "stop" the cracker execution, return the last generated value, and after start it again from where it stopped.

Is it clear ?

Thanks,
Root.
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12123763
Sure. Give me a few minutes. =)
[r.D]
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12124028
Okay, I just had one idea of doing it, and it failed. lol :(
However, I've got another idea, which is sure to work :D (I hope :P).

So, if you just give me about 10 mins or so, and it should be done :) ....

[r.D]
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12124378
Okay, I've just finished it.. and it works! YAY! :D

Here you go:

//------------------------------------ Cracker.java ----------------------------------------------\\
public class Cracker
{
     private String ALPHABET[] = { "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
     private int LIMIT = 3;
     private String sPass = null;
     private boolean bPass = false;
     private boolean done = false;
     
     /*\
      * Constructor for Cracker.
     \*/
     public Cracker( String sArg )
     {
          super();
          int LIMIT2 = LIMIT;
          int b;
          if ( sArg.length() > LIMIT2 ) {
            System.out.println( "\nError: Argument length must be between 1 and " + LIMIT );
            System.exit(1);
          }
         
          if ( sArg != "-" ) { bPass = true; sPass = sArg; b = sArg.length()-1; } else b = 0;
          for ( int a = b; a < LIMIT2; a++ ) {
               LIMIT = a+1;
               for(int i=0; i<ALPHABET.length; i++)
                    cracker(1, ALPHABET[i]);
          }
     }
   
     public static void main(String[] args)
     {
          if ( args.length > 0 ) new Cracker( args[0] ); else new Cracker( "-" );
     }
     
     public void cracker(int index, String password)
     {
          if ( bPass && !done ) { password = sPass; done = true; }
          if ( password.length() == LIMIT )
               System.out.println(password);
          if(index == LIMIT)
               return;
          for(int i=0; i<ALPHABET.length; i++)
               cracker(index+1, password+ALPHABET[i]);
     }
}
//------------------------------------ Cracker.java ----------------------------------------------\\


Best of luck,
[r.D]
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12124389
Just use it like so (at Command Prompt):

java Cracker AAC

(Replace AAC with the generated value you want to continue from :))
0
 
LVL 3

Author Comment

by:root_start
ID: 12124995
Ok... Thanks again...

Sorry to ask you so much, but how can I return the last generated value ?

Thanks,
Root.
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12125244
Well, that depends on how you're using the program.. are you simply using "java Cracker" at command prompt? Or something else?

[r.D]
0
 
LVL 3

Author Comment

by:root_start
ID: 12125445
I need to test all possible values in another method, because of it, I need to return the last value.

Root.
0
 
LVL 9

Accepted Solution

by:
DrWarezz earned 50 total points
ID: 12125698
Oh right, so; you're going to replace the "System.out.println( password );" (outputting each password) with a method, which will then process the password?

If so, then you want to output the password about to be process before exiting.
For that, I would recommend using a JFrame, then adding the code to output the last password before closing.

However, if you really can't be bothered to do that, then perhaps set it to keep outputting each password, overwriting the previous into a file (I recommend using a thread to do so, so that it does't take forever to process each one):

//------------------------------------ Cracker.java ----------------------------------------------\\
public class Cracker
{
     private String ALPHABET[] = { "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
     private int LIMIT = 3;
     private String sPass = null;
     private boolean bPass = false;
     private boolean done = false;
     public String sFileName = "FinalPassword.txt"
     
     /*\
      * Constructor for Cracker.
     \*/
     public Cracker( String sArg )
     {
          super();
          int LIMIT2 = LIMIT;
          int b;
          if ( sArg.length() > LIMIT2 ) {
            System.out.println( "\nError: Argument length must be between 1 and " + LIMIT );
            System.exit(1);
          }
         
          if ( sArg != "-" ) { bPass = true; sPass = sArg; b = sArg.length()-1; } else b = 0;
          for ( int a = b; a < LIMIT2; a++ ) {
               LIMIT = a+1;
               for(int i=0; i<ALPHABET.length; i++)
                    cracker(1, ALPHABET[i]);
          }
     }
   
     public static void main(String[] args)
     {
          if ( args.length > 0 ) new Cracker( args[0] ); else new Cracker( "-" );
     }
     
     public void cracker(int index, String password)
     {
          if ( bPass && !done ) { password = sPass; done = true; }
          if ( password.length() == LIMIT )
               // Process, by calling the method you want, here.
               Thread t = new Thread( new OutputClass( password ) ); // Create new thread to output password to file.
               t.start(); // Start thread.
          if(index == LIMIT)
               return;
          for(int i=0; i<ALPHABET.length; i++)
               cracker(index+1, password+ALPHABET[i]);
     }
     
     public class OutputClass extends Thread {
          private String sPass;
          public OutputClass( String sP ) { sPass = sP; }
          public void run () {
               if ((new File( sFileName )).exists()) {
                    try {
                         BufferedWriter out = new BufferedWriter(new FileWriter( sFileName ));
                         out.write( sPass );
                         out.close();
                    } catch (Exception e) {}
               } else {
                    try {
                         File file = new File("filename");
                         file.createNewFile();
                    } catch (Exception e) {}
                    try {
                         BufferedWriter out = new BufferedWriter(new FileWriter( sFileName ));
                         out.write( sPass );
                         out.close();
                    } catch (Exception e) {}
               }
          }
     }
}
//------------------------------------ Cracker.java ----------------------------------------------\\

There you go.
I haven't tested it, but if there are any bugs, I'm sure they're very easily fixed :)

Hope it helps,
[r.D]
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12125726
Woops, one problem, replace:

if ( password.length() == LIMIT )
   // Process, by calling the method you want, here.
   Thread t = new Thread( new OutputClass( password ) ); // Create new thread to output password to file.
   t.start(); // Start thread.

with:

if ( password.length() == LIMIT ) {
   // Process, by calling the method you want, here.
   Thread t = new Thread( new OutputClass( password ) ); // Create new thread to output password to file.
   t.start(); // Start thread.
}

:)
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12125779
Then, no matter when you exit the program, the most recent password will have been outputted to the file that you can specify:

public String sFileName = "FinalPassword.txt"

#Obviously, it's cirrently 'FinalPassword.txt'. And will be made in the same directory that Cracker.class is in.
However, if you wish to change the directory that the file is made in, then you need to add the new directory, remembering to use double '\' instead of single. eg:
public String sFileName = "c:\\Cracker\\RecentPassword\\FinalPassword.txt"

If you'd rather avoid outputting to a file, for whatever reason, then just say, and I'll give you a hand with creating something else, such as a JFrame. :)

[r.D]
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12401304
ta :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question