Java Scripts

Java Brute force

You guys know whats happening. Its another brute force, but in Java. From a main menu there are two options, Sequential and Random. Sequential tries to guess the password from 0000... to 9999... while the random method guesses random numbers. If you were implementing this to actually do something useful, if using random you would want to record the tried passwords and not use those again, to not repeat trials.

Try it out!
import java.util.Random;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("");
        System.out.println("     ██╗ █████╗ ██╗   ██╗ █████╗     ██████╗ ██████╗ ██╗   ██╗████████╗███████╗");
        System.out.println("     ██║██╔══██╗██║   ██║██╔══██╗    ██╔══██╗██╔══██╗██║   ██║╚══██╔══╝██╔════╝");
        System.out.println("     ██║███████║██║   ██║███████║    ██████╔╝██████╔╝██║   ██║   ██║   █████╗  ");
        System.out.println("██   ██║██╔══██║╚██╗ ██╔╝██╔══██║    ██╔══██╗██╔══██╗██║   ██║   ██║   ██╔══╝  ");
        System.out.println("╚█████╔╝██║  ██║ ╚████╔╝ ██║  ██║    ██████╔╝██║  ██║╚██████╔╝   ██║   ███████╗");
        System.out.println(" ╚════╝ ╚═╝  ╚═╝  ╚═══╝  ╚═╝  ╚═╝    ╚═════╝ ╚═╝  ╚═╝ ╚═════╝    ╚═╝   ╚══════╝");
        System.out.println("By Elliot");
        System.out.println();
        System.out.println("(1) Sequential");
        System.out.println("(2) Random");
        int choice = in.nextInt();
        if (choice == 1) {
            sequentialBrute();
        } else if ( choice == 2) {
            randomBrute();
        }
        clearScreen();
    }
    public static void sequentialBrute() {
        Random rand = new Random();
        Scanner in = new Scanner(System.in);
        System.out.println("Random code? (true / false)");
        boolean random = in.nextBoolean();
        boolean randomMode;
        randomMode = random;
        System.out.println("Enter # digits in code: ");
        int length = in.nextInt();
        int max = findMax(length);
        int codeUnformatted;
        if (randomMode) {
            codeUnformatted = rand.nextInt(max);
        } else {
            System.out.println("Enter code to break: ");
            codeUnformatted = in.nextInt();
        }
        String code = convertToXDigits(codeUnformatted, length);
        for(int trial = 1; trial < (max + 1); trial++){
            String codeTrial = convertToXDigits(trial, length);
            System.out.println(codeTrial);
            if (codeTrial.equals(code)) {
                System.out.println("Found!");
                break;
            }
        }
    }
    public static void clearScreen() {
        System.out.print("\033[H\033[2J");
        System.out.flush();
    }
    public static void randomBrute() {
        Random rand = new Random();
        Scanner in = new Scanner(System.in);
        System.out.println("Random code? (true / false)");
        boolean random = in.nextBoolean();
        boolean randomMode;
        randomMode = random;
        System.out.println("Enter # digits in code: ");
        int length = in.nextInt();
        int max = findMax(length);
        int codeUnformatted = 0;
        if (randomMode) {
            codeUnformatted = rand.nextInt(max);
        } else {
            System.out.println("Enter code: ");
            codeUnformatted = in.nextInt();
        }
        String code = convertToXDigits(codeUnformatted, length);
        String trial = "0";
        int count = 0;
        while (!trial.equals(code)) {
            count = count + 1;
            int trialCode = rand.nextInt(max);
            trial = String.format("%0" + length + "d", trialCode);
            System.out.println(trial);
        }
        System.out.println("Found");
        System.out.printf("Attempts: " + count);
    }
    public static String convertToXDigits(int trialCode, int length) {
        return String.format("%0" + length + "d", trialCode);
    }
    public static int findMax(int length) {
        String nines = "";
        for(int i=1; i<(length + 1); i++){
            nines = nines + "9";
        }
        return Integer.parseInt(nines);
    }
}

Domino#3868

14s0c@protonmail.com