Pages

Wednesday, October 7, 2015

Generate list of Permutation of N length



In cryptography, a brute-force attack, or exhaustive key search, is a cryptanalytic attack that can, in theory, be used against any encrypted data.

Brute force attacks work by calculating every possible combination that could make up a password and testing it to see if it is the correct password. As the password’s length increases, the amount of time, on average, to find the correct password increases exponentially. This means short passwords can usually be discovered quite quickly, but longer passwords may take decades.

String list is A-z. Length is 26 + 26 = 52 character.

the combination of 5 character is

52 * 52 * 52 * 52 *52 = 52^5 = 380204032 combination !!!


Below is the example of brute force.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.*;
/*
   modified by hafiq
   Brute force list ex: a..aaab..aaaaaaC....
   any expert can advise me to improve this code...tq..
*/
public class Bruteforce
{
  public static void main(String[] args){
      String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
      int max = 4; //limit length
      generate(str.toCharArray(),max);
  }
  public static void generate(char[] input,int max) {
    char[] result = new char[input.length];
    int[] index = new int[input.length];

    // initialize the arrays.
    Arrays.fill(result, 0, result.length, input[0]);
    Arrays.fill(index,  0, index.length, 0);

    // loop over the output lengths.
    for( int length = 1; length <= max; length++ ) {
      int update = 0;
      
      while(update != -1){
        System.out.println(new String(result, 0, length));

        // update values that need to reset.
        for(update = length-1;
            update != -1 && ++index[update] == input.length;
            result[update] = input[0], index[update] = 0, update--);

        // update the character that is not resetting, if valid
        if( update != -1 ) 
            result[update] = input[index[update]];
      } 
    }
  }
}

No comments:

Post a Comment