Solved

Java - Alternating Values like a Password Cracker.

Posted on 2004-09-21
21
240 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
  • 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now