I have to build a program which takes a password as input and then the password machine hacks it. It should do it by looping through the ascii character map 4 times i.e a, b ,c etc followed be aa, ab, ac. It's meant to simulate a "brute force attack". Can you offer me any advice in checking the ascii map in this way using a loop and also I need to return the amount of time taken for it to hack it. Is there a way I can return the seconds, or will I have to simulate it? Thanks!

You need 4 nested (identical) loops.
The System class has a method that gives you the current time in mSec. Call it before and after, subtract the two for elapsed time.

Have a go, see how far you get, come back here if you get stuck (post what you hace done so far)

Apologies, I am fairly new to Java so I don't really understand how the strings work, or how to use the libraries. I think what I need now is a way of finding the individual characters of the password String entered by the user. I also need a way to check if there is a character present, as I am probably going to add more for loops.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package password.program;

/**
 *
 * @author Jack
 */
public class Main 
{
public String[] ascii;
 public long startTime;
    public long endTime;
    public int check1;
    public int check2;
    public int check3;
    public int check4;


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
      String[]  ascii = {
        "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",
        "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 void Checker(String Password)
    {
        startTimer();
        int i;
        for(i=0; i<=62; i++)
        {


            for(i=0; i<=62; i++)
            {
                for(i=0; i<=62; i++)
                {
                    for(i=0; i<=62; i++)
                    {

                    }
                }
            }
        }

        endTimer();

    }

    public void startTimer()
    {
        startTime = System.currentTimeMillis();  
    }
    public void endTimer()
    {
     endTime = System.currentTimeMillis() - startTime; 
    }
    public long returnTimer()
    {
        return endTime;
    }




}

Thanks!

OK, you are making a decent effort, so there are plenty of people here will wiull be happy to help.

Beware line 27 - that declares a new variable called "ascii", so it won't initialise the one you declared on line 13.
Beware also that you are creating instance variables, but trying to use them froma static method.

Don't be afraid to put your code through the compiler as soon as possible. It's the quickest possible way to find errors. The code you posted isn't complete, so it won't work properly, but with the errors fixed it should compile.

Inside your inner-most loop you will want to put together a 4-letter String by concatenating letters from your array. Each loop will contribute a different letter, so you will need different looop variables for each loop. You can concatenate Strings with the + operator, so it goes along these lines...

for (int i = .....
   for (int j = ...
      ...
      String try = ascii[i] + ascii[j] ...

(Using an array of 1-letter Strings like this is perfectly valid, but it is going to be very inefficient. But I recommend that for now you ignore efficiency and get something, anything, that works. Then you can try smarter data structures or algorithms and you will be able to measure now much faster they are. Just go one step at a time.)

Ok, this is what I have so far. The GUI provides the length of the password (int) and the password as an array of chars when the button is clicked:

userPass = jTextField1.getText();
       jTextField2.setText("");
        String str = userPass;
        char[] cArray = str.toCharArray();
        passwordLength = cArray.length;
        Checker Check = new Checker(passwordLength, cArray);
        jTextField2.setText(Check.Checker());

and then this is the checker class. I can't see what's wrong, but it just seems to be throwing exceptions here, there and everywhere!

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package password.program;

/**
 *
 * @author Jack
 */
public class Checker {
    public char[] ascii = {
        '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',
        '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 long startTime;
    public long endTime;
    public int check1;
    public int check2;
    public int check3;
    public int check4;
    public int passLength;
    public char[] outPass;
    public char[] inPass;



   public Checker(int pass, char[] userPass)
   {
       passLength = pass;
       inPass = userPass;

   }

    public String Checker() {


        startTimer();
        int i;
        if (passLength >= 1) {
            for (i = 0; i <= 62; i++) {
                if (ascii[i] == inPass[0]) {
                    outPass[0] = ascii[i];
                    i=100;
                }

            }
        } else {
            return "Please enter a Password";
        }
        int j;
        if (passLength >= 2) {
            for (j = 0; j <= 62; j++) {
                if (ascii[j] == inPass[1]) {
                    outPass[1] = ascii[j];
                    j=100;
                }

            }
        }
        int k;
        if(passLength >= 3)
        {
            for(k=0; k<=62; k++)
            {
            if(ascii[k]==inPass[2])
            {
               outPass[2] = ascii[k];
               k=100;
            }

            }
        }
        int l;
        if(passLength >= 4)
        {
            for(l=0; l<=62; l++)
            {
            if(ascii[l]==inPass[3])
            {
               outPass[3] = ascii[l];
               l=100;
            }

            }
        }
        endTimer();
        String s = outPass.toString();
        return s;
    }

    public void startTimer() {
        startTime = System.currentTimeMillis();
    }

    public void endTimer() {
        endTime = System.currentTimeMillis() - startTime;
    }

    public long returnTimer() {
        return endTime;
    }

}
This article has been dead for over six months. Start a new discussion instead.