What is the ForceBrute logic of trial and error?

2

How is it done to make the billions of combinations possible without loss of time?

I made a random letter generation system (it can do all the combinations), but it loses a lot of time checking results already made by being random, and then I made a system with for but only up to 2 characters.

This is what I have, as I said, I only managed with 2 digits.

public class Main 
{
static boolean terminar = false;

public static void main(String[] args)
{
    long init  = System.currentTimeMillis(); 

    if(bruteForce(true, true, "0dd", 10, 10000) != "Senha não encontrada")
    {
        System.out.println("A senha é: " + bruteForce(true, true, "0dd", 10, 10000));
    }
    else
    {
        System.out.println("Senha não encontrada"); 
    }

    long end  = System.currentTimeMillis(); 
    long diff = end - init;
    System.out.println("Demorou " + (diff / 1000) + " segundos");
}
public static String bruteForce(boolean numeros, boolean letras, String busca,int maxCaracteres,  int limite)
{
    String[] todos = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "a", "b", "c", "d", "e", "f", "g"
                      , "h", "i", "j", "k", "l", "m", "n", "o", "p", "k", "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", "'", "!", "@", "#", "$", "£", "%", "¢", "¨", "¬", "&", "*", "(", ")", "_", "-"
                      , "+", "=", "§", "/", ".", "[", "]", "'", "´", "{", "}", "º", "^", "~", "<", ">", ":", ";", "?", "°", "ª",
                      "|", "¹", "²", "³"};

    String combinacao = "";
    int quantCaracteres = 2;
    double teste = Math.pow(todos.length, 16);
    System.out.println(teste);  

    for(int i = 1; i <= quantCaracteres; i++)
    {
        for(int h = 0; h <= todos.length -1; h++)
        {
            for(int j = 0; j <= todos.length -1; j++)
            {
                combinacao = todos[h] + todos[j];
                System.out.println(combinacao); 
                if(combinacao.equals(busca))
                {
                    return combinacao; 
                }

                combinacao = "";
            }
        }

        //quantCaracteres ++;
    }
    return "Senha não encontrada";
}
}
    
asked by anonymous 07.07.2015 / 18:52

2 answers

3

I found this code that can help you. I'm not going to assure you it's the best shape but it's certainly a good start:

import java.util.Arrays;

public class BruteForce {

  final int min;
  final int max;
  final int stringLength;

  /**
   * One more element than <i>stringLength</i>,
   * to efficiently check for overflow.
   */
  private final int[] chars;

  public BruteForce(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);
  }

  public void run() {
    while (chars[0] == 0) {
      print();
      increment();
    }
  }

  private void increment() {
    for (int i = chars.length - 1; i >= 0; i--) {
      if (chars[i] < max) {
        chars[i]++;
        return;
      }
      chars[i] = min;
    }
  }

  private void print() {
    for (int i = 1; i < chars.length; i++) {
      System.out.print((char) chars[i]);
    }
    System.out.println();
  }

  public static void main(String[] args) {
    new BruteForce('a', 'z', 4).run();
  }

}

Font .

It has simpler shapes than this one. It is possible to do with only two for nested, one to increase the position of the character and the other to increment the character itself. But it can even do with just one loop if you use creativity.

There are more advanced brute force techniques but they play with probabilities. For something simple the fact is that you need to keep the sequence. What you can not do is go kicking random values, this will probably give you worse results in most attempts.

    
07.07.2015 / 19:10
0

So, from what you said, your code tests the same combination several times. One way to decrease it is to save the combinations and check if it has already been tested. Home You can also use a dictionary of combinations ...
Here is a link that might help you:
link

    
07.07.2015 / 19:11