1

I need some help with my program. I'm trying to make a brute force program as a project and I don't know how to make the loop values appear in the JTextField in the print () method. I don't just need the final value of the loop. I know this question has been asked a lot and I've tried everything but it's just not working. Please can anyone help me? I'll appreciate it !

public class BF  implements ActionListener {
private JPanel contentPane;
private JTextField password;
private JTextField length;
public JTextField generateField;
JRadioButton numbers;
JButton generate;
JLabel passwordfound;
JLabel timelabel;
int min;
int max; 
int stringLength; 
private  int[] chars;
String n="";
char[] passCom;
JFrame frame;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                BF frame = new BF();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public BF() {
     frame = new JFrame ("Brute Force Attack Program");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(100, 100, 754, 665);

    frame.setResizable(false);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    frame.setLocationRelativeTo(null);
    frame.setContentPane(contentPane);
    contentPane.setLayout(null);
    try {
        UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch(Exception e) {   
    }
    backgroundPanel panel = new backgroundPanel();
    panel.setBounds(0, 0, 749, 633);
    contentPane.add(panel);
    panel.setLayout(null);

    generate = new JButton("Find Password");
    generate.setFont(new Font("Tahoma", Font.BOLD, 15));
    generate.setBounds(292, 401, 145, 35);
    generate.addActionListener(this);
    panel.add(generate);

    generateField = new JTextField();
    generateField.setForeground(Color.WHITE);
    generateField.setFont(new Font("Tahoma", Font.BOLD, 24));
    generateField.setEditable(false);
    generateField.setBounds(315, 449, 186, 54);
    generateField.setBackground(new Color(0,0,0,0));
    generateField.setBorder(javax.swing.BorderFactory.createEmptyBorder());
    panel.add(generateField);
    generateField.setColumns(10);


    frame.setVisible(true);


}
public void run(char min, char max, int len)

{
    this.min = min;

    this.max = max;

    this.stringLength = len;

    chars = new int[stringLength + 1];

    Arrays.fill(chars, 1, chars.length, min);


  while (chars[0] == 0&&!n.equals(password.getText()))

  {


 print();

 increment();
 n=String.valueOf(passCom);

  }


}

public void increment()

{

  for (int i = chars.length - 1; i >= 0; i--)

  {

   if (chars[i] < max)

   {

    chars[i]++; return;

    }

  chars[i] = min;

  }

}

public void print()

{
   int x=0;
   for (int i = 1; i < chars.length; i++)

  {
    passCom[x]=(char)chars[i];
     x++;
    String txt = new String (passCom);
    generateField.setText(txt);


  }

}




@Override
public void actionPerformed(ActionEvent e) {

    if (!password.getText().equals("") && e.getSource() == generate && !length.getText().equals("")&&numbers.isSelected()){
        int count = Integer.parseInt(length.getText());
        passCom = new char [count];
            run('0','9',count);
  }

  }
}
Sarah_b
  • 31
  • 9
  • Try adding a delay `Thread.sleep(1000)` where 1000 is millisecond – Tin Apr 20 '16 at 13:39
  • What this `backgroundPanel panel = new backgroundPanel();` do? – bili Apr 20 '16 at 13:45
  • Your code blocks are not runnable. And What do you want in the loop actually ? – ziLk Apr 20 '16 at 13:51
  • @bili it's a custom jpanel – Sarah_b Apr 20 '16 at 13:53
  • ok. So if the `generatetField` prints the final value in in the loop then it's most likely the text get updated so quickly you cannot see it displayed in between changes until the final value. Have you tried Tin's suggestion? You can also add a print of the text before calling `enerateField.setText(txt); to see the text before each change` – bili Apr 20 '16 at 14:00
  • @zilk I want the loop to print all the permutations of a string of numbers. For example, the string 123 has length 3 so the permutations can be 000,001,002 etc. The loop will print those I just need to show them in the JTextField – Sarah_b Apr 20 '16 at 14:04
  • @Sarah_b then you just need to concat those previously permutate text into a string then instead of calling `generateField.setText(txt);` do `generateField.setText(results);`. So each time the loop will add the new permutate text to this string. But it probably won't fit into the JTextField for a long string. – bili Apr 20 '16 at 14:11
  • @bili I want to show one value at a time – Sarah_b Apr 20 '16 at 14:52

1 Answers1

1

For example, the string 123 has length 3 so the permutations can be 000,001,002 etc

Then why only display one at t time?

Instead I would use a JTextArea and append each value to the text field so all values are visible at the same time. Nobody is going to remember all the values if you display one at a time.

Otherwise, you would use a Swing Timer to display one at a time.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I don't want to show all the values at once I need one value at a time. – Sarah_b Apr 20 '16 at 14:50
  • Even if you want them one at a time you should be using the Timer. Then you just use the `setText()` method to replace the last value. – camickr Apr 20 '16 at 15:01
  • How can I implement it in my program? I've never used timers before – Sarah_b Apr 20 '16 at 16:58
  • @Sarah_b, read the tutorial for an example, that is why you were given the link. You can also search the forum for examples that use a Timer. For example: http://stackoverflow.com/questions/30417719/update-a-label-with-a-swing-timer/30417857#30417857 – camickr Apr 20 '16 at 19:18