Java - Alternating Values like a Password Cracker.

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.
LVL 3
root_startAsked:
Who is Participating?
 
DrWarezzCommented:
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
 
nesnemisCommented:
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
 
DrWarezzCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
root_startAuthor Commented:
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
 
DrWarezzCommented:
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
 
root_startAuthor Commented:
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
 
DrWarezzCommented:
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
 
DrWarezzCommented:
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
 
root_startAuthor Commented:
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
 
DrWarezzCommented:
Sure. Give me a few minutes. =)
[r.D]
0
 
DrWarezzCommented:
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
 
DrWarezzCommented:
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
 
DrWarezzCommented:
Just use it like so (at Command Prompt):

java Cracker AAC

(Replace AAC with the generated value you want to continue from :))
0
 
root_startAuthor Commented:
Ok... Thanks again...

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

Thanks,
Root.
0
 
DrWarezzCommented:
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
 
root_startAuthor Commented:
I need to test all possible values in another method, because of it, I need to return the last value.

Root.
0
 
DrWarezzCommented:
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
 
DrWarezzCommented:
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
 
DrWarezzCommented:
ta :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.