Advertisement
LegarsStyler

BruteForce

May 4th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. package net.redheademile.bruteforce;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.Scanner;
  8. import java.util.stream.Collectors;
  9.  
  10. public class BruteForce
  11. {
  12.     public static void main(String[] args) throws IOException
  13.     {
  14.         Scanner in = new Scanner(System.in);
  15.         char[] chars;
  16.         int min, max, thread;
  17.         String lo = "abcdefghijklmnopqrstuvwxyz", up = lo.toUpperCase(), number = "0123456789", sp1 = "&éè!?à@ç_-", sp2 = "§:/;.,*$^ùµ%¨£¤+°}]`|[{#~=)('\"\\", all = "";
  18.        
  19.         while(true)
  20.         {
  21.             try
  22.             {
  23.                 System.out.print("number of character (n | <min-max>): ");
  24.                 String str = in.nextLine();
  25.                 if(str.contains("-"))
  26.                 {
  27.                     String[] n = str.split("-");
  28.                     min = Integer.valueOf(n[0]);
  29.                     max = Integer.valueOf(n[1]);
  30.                 }
  31.                 else
  32.                 {
  33.                     min = Integer.valueOf(str);
  34.                     max = min;
  35.                 }
  36.                 System.out.print("Number of thread: ");
  37.                 thread = Integer.valueOf(in.nextLine());
  38.                 break;
  39.             }
  40.             catch(NumberFormatException e)
  41.             {
  42.                 System.out.println("Enter valid number !");
  43.             }
  44.         }
  45.        
  46.         System.out.print("Lower case ? (y/n): ");
  47.         if(in.nextLine().toLowerCase().startsWith("y")) all += lo;
  48.        
  49.         System.out.print("Upper case ? (y/n): ");
  50.         if(in.nextLine().toLowerCase().startsWith("y")) all += up;
  51.        
  52.         System.out.print("Number chars ? (y/n): ");
  53.         if(in.nextLine().toLowerCase().startsWith("y")) all += number;
  54.        
  55.         System.out.print("Common special chars ? (y/n): ");
  56.         if(in.nextLine().toLowerCase().startsWith("y")) all += sp1;
  57.        
  58.         System.out.print("Other special chars ? (y/n): ");
  59.         if(in.nextLine().toLowerCase().startsWith("y")) all += sp2;
  60.         in.close();
  61.  
  62.         long startat = System.currentTimeMillis();
  63.        
  64.         File file = new File("E:\\out", System.currentTimeMillis() + ".txt");
  65.         if(!file.getParentFile().exists()) file.getParentFile().mkdirs();
  66.         FileWriter out;
  67.         try
  68.         {
  69.             if(!file.exists()) file.createNewFile();
  70.             out = new FileWriter(file);
  71.         }
  72.         catch (Exception e) { System.out.println("Can't use the file"); return; }
  73.        
  74.         chars = all.toCharArray();
  75.         ArrayList<BruteThread> bts = new ArrayList<>();
  76.         for(int i = 0; i < thread; i++)
  77.         {
  78.             BruteThread bt = new BruteThread()
  79.             {
  80.                 @Override
  81.                 public void run()
  82.                 {
  83.                     for(int i = min; i <= max; i++)
  84.                     {
  85.                         char[] pass = new char[i];
  86.                         for(int j = 0; j < pass.length; j++) pass[j] = chars[0];
  87.                         pass = increment(pass, chars, startat, i - 1);
  88.                         String passSTR = new String(pass);
  89.                         System.out.println(passSTR);
  90.                         try
  91.                         {
  92.                             out.append(passSTR + '\n');
  93.                         }
  94.                         catch (IOException e) { e.printStackTrace(); }
  95.                        
  96.                         double total = Math.pow(chars.length, i) / (double) increment;
  97.                         for(int k = 1; k < total; k++)
  98.                         {
  99.                             pass = increment(pass, chars, increment, pass.length - 1);
  100.                             passSTR = new String(pass);
  101.                             System.out.println(passSTR);
  102.                             try
  103.                             {
  104.                                 out.append(passSTR + '\n');
  105.                             }
  106.                             catch (IOException e) { e.printStackTrace(); }
  107.                         }
  108.                     }
  109.                     finished = true;
  110.                 }
  111.                
  112.                 private char[] increment(char[] from, char[] alphabet, int step, int pos)
  113.                 {
  114.                     char[] to = from;
  115.                     int posInA = 0;
  116.                     for(int i = 0; i < alphabet.length; i++)
  117.                         if(alphabet[i] == from[pos])
  118.                         {
  119.                             posInA = i;
  120.                             break;
  121.                         }
  122.                     posInA += step;
  123.                     int dizPlus = posInA / alphabet.length;
  124.                     if(posInA >= alphabet.length && pos - 1 >= 0) increment(to, alphabet, dizPlus, pos - 1);
  125.                     to[pos] = alphabet[posInA > alphabet.length - 1 ? posInA - (alphabet.length * dizPlus) : posInA];
  126.                     return to;
  127.                 }
  128.             };
  129.             bt.setChars(chars);
  130.             bt.setIncrement(thread);
  131.             bt.setStartAt(i);
  132.             bt.setMinAndMax(min, max);
  133.             bt.start();
  134.             bts.add(bt);
  135.         }
  136.        
  137.         while(bts.stream().map(bt -> bt.finished).collect(Collectors.toList()).contains(false));
  138.         out.close();
  139.        
  140.         System.out.println("\nFinished ! (" + (System.currentTimeMillis() - startat) + "ms)");
  141.     }
  142.    
  143.     private static class BruteThread extends Thread
  144.     {
  145.         protected boolean finished = false;
  146.         protected char[] chars;
  147.         protected int increment, startat;
  148.         protected int min, max;
  149.        
  150.         public void setChars(char[] chars)
  151.         {
  152.             this.chars = chars;
  153.         }
  154.        
  155.         public void setIncrement(int increment)
  156.         {
  157.             this.increment = increment;
  158.         }
  159.        
  160.         public void setStartAt(int startat)
  161.         {
  162.             this.startat = startat;
  163.         }
  164.        
  165.         public void setMinAndMax(int min, int max)
  166.         {
  167.             this.min = min;
  168.             this.max = max;
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement