Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.redheademile.bruteforce;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class BruteForce
- {
- public static void main(String[] args) throws IOException
- {
- Scanner in = new Scanner(System.in);
- char[] chars;
- int min, max, thread;
- String lo = "abcdefghijklmnopqrstuvwxyz", up = lo.toUpperCase(), number = "0123456789", sp1 = "&éè!?à@ç_-", sp2 = "§:/;.,*$^ùµ%¨£¤+°}]`|[{#~=)('\"\\", all = "";
- while(true)
- {
- try
- {
- System.out.print("number of character (n | <min-max>): ");
- String str = in.nextLine();
- if(str.contains("-"))
- {
- String[] n = str.split("-");
- min = Integer.valueOf(n[0]);
- max = Integer.valueOf(n[1]);
- }
- else
- {
- min = Integer.valueOf(str);
- max = min;
- }
- System.out.print("Number of thread: ");
- thread = Integer.valueOf(in.nextLine());
- break;
- }
- catch(NumberFormatException e)
- {
- System.out.println("Enter valid number !");
- }
- }
- System.out.print("Lower case ? (y/n): ");
- if(in.nextLine().toLowerCase().startsWith("y")) all += lo;
- System.out.print("Upper case ? (y/n): ");
- if(in.nextLine().toLowerCase().startsWith("y")) all += up;
- System.out.print("Number chars ? (y/n): ");
- if(in.nextLine().toLowerCase().startsWith("y")) all += number;
- System.out.print("Common special chars ? (y/n): ");
- if(in.nextLine().toLowerCase().startsWith("y")) all += sp1;
- System.out.print("Other special chars ? (y/n): ");
- if(in.nextLine().toLowerCase().startsWith("y")) all += sp2;
- in.close();
- long startat = System.currentTimeMillis();
- File file = new File("E:\\out", System.currentTimeMillis() + ".txt");
- if(!file.getParentFile().exists()) file.getParentFile().mkdirs();
- FileWriter out;
- try
- {
- if(!file.exists()) file.createNewFile();
- out = new FileWriter(file);
- }
- catch (Exception e) { System.out.println("Can't use the file"); return; }
- chars = all.toCharArray();
- ArrayList<BruteThread> bts = new ArrayList<>();
- for(int i = 0; i < thread; i++)
- {
- BruteThread bt = new BruteThread()
- {
- @Override
- public void run()
- {
- for(int i = min; i <= max; i++)
- {
- char[] pass = new char[i];
- for(int j = 0; j < pass.length; j++) pass[j] = chars[0];
- pass = increment(pass, chars, startat, i - 1);
- String passSTR = new String(pass);
- System.out.println(passSTR);
- try
- {
- out.append(passSTR + '\n');
- }
- catch (IOException e) { e.printStackTrace(); }
- double total = Math.pow(chars.length, i) / (double) increment;
- for(int k = 1; k < total; k++)
- {
- pass = increment(pass, chars, increment, pass.length - 1);
- passSTR = new String(pass);
- System.out.println(passSTR);
- try
- {
- out.append(passSTR + '\n');
- }
- catch (IOException e) { e.printStackTrace(); }
- }
- }
- finished = true;
- }
- private char[] increment(char[] from, char[] alphabet, int step, int pos)
- {
- char[] to = from;
- int posInA = 0;
- for(int i = 0; i < alphabet.length; i++)
- if(alphabet[i] == from[pos])
- {
- posInA = i;
- break;
- }
- posInA += step;
- int dizPlus = posInA / alphabet.length;
- if(posInA >= alphabet.length && pos - 1 >= 0) increment(to, alphabet, dizPlus, pos - 1);
- to[pos] = alphabet[posInA > alphabet.length - 1 ? posInA - (alphabet.length * dizPlus) : posInA];
- return to;
- }
- };
- bt.setChars(chars);
- bt.setIncrement(thread);
- bt.setStartAt(i);
- bt.setMinAndMax(min, max);
- bt.start();
- bts.add(bt);
- }
- while(bts.stream().map(bt -> bt.finished).collect(Collectors.toList()).contains(false));
- out.close();
- System.out.println("\nFinished ! (" + (System.currentTimeMillis() - startat) + "ms)");
- }
- private static class BruteThread extends Thread
- {
- protected boolean finished = false;
- protected char[] chars;
- protected int increment, startat;
- protected int min, max;
- public void setChars(char[] chars)
- {
- this.chars = chars;
- }
- public void setIncrement(int increment)
- {
- this.increment = increment;
- }
- public void setStartAt(int startat)
- {
- this.startat = startat;
- }
- public void setMinAndMax(int min, int max)
- {
- this.min = min;
- this.max = max;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement