暴力破解密码 Java

Brute force password cracker Java

我正在为 class 做作业,我必须在 java 中创建一个暴力密码破解器。

  1. 使用递归编写一个函数来破解密码。密码长度未知(最多10位),由大写字母和数字组成。 (在你的程序中存储实际的密码,只是为了检查当前获取的字符串是否是正确的密码。)

我的解决方案是:

import java.util.Arrays;

public class BruteForce {
  public static void main(String[] args) {
    bruteForce(3);
  }

  public static String bruteForce(int size) {
    int[] password = new int[size];
    String[] finalPassword = new String[size];
    for (int i = 0; i < size; i++) {
      password[i] = 0;
      finalPassword[i] = "";
    }
    String pass = "AAA";
    return computePermutations(size, password, 0, pass);
  }

  private static String computePermutations(int size, int[] password, int position, String pass) {
    String testString = "";
    String assemble = "";
    for (int i = 0; i < 36; i++) {
      password[position] = i;

      if (position != size - 1) {
        testString = computePermutations(size, password, position + 1, pass);
        if (testString != "") {
          return testString;
        }
      } else if (position == size - 1) {
        for (int j = 0; j < size; j++) {

          switch (password[j] + 1) {
            case 1:
              assemble = assemble + "A";
              break;
            case 2:
              assemble = assemble + "B";
              break;
            case 3:
              assemble = assemble + "C";
              break;
            case 4:
              assemble = assemble + "D";
              break;
            case 5:
              assemble = assemble + "E";
              break;
            case 6:
              assemble = assemble + "F";
              break;
            case 7:
              assemble = assemble + "G";
              break;
            case 8:
              assemble = assemble + "H";
              break;
            case 9:
              assemble = assemble + "I";
              break;
            case 10:
              assemble = assemble + "J";
              break;
            case 11:
              assemble = assemble + "K";
              break;
            case 12:
              assemble = assemble + "L";
              break;
            case 13:
              assemble = assemble + "M";
              break;
            case 14:
              assemble = assemble + "N";
              break;
            case 15:
              assemble = assemble + "O";
              break;
            case 16:
              assemble = assemble + "P";
              break;
            case 17:
              assemble = assemble + "Q";
              break;
            case 18:
              assemble = assemble + "R";
              break;
            case 19:
              assemble = assemble + "S";
              break;
            case 20:
              assemble = assemble + "T";
              break;
            case 21:
              assemble = assemble + "U";
              break;
            case 22:
              assemble = assemble + "V";
              break;
            case 23:
              assemble = assemble + "W";
              break;
            case 24:
              assemble = assemble + "X";
              break;
            case 25:
              assemble = assemble + "Y";
              break;
            case 26:
              assemble = assemble + "Z";
              break;
            case 27:
              assemble = assemble + "0";
              break;
            case 28:
              assemble = assemble + "1";
              break;
            case 29:
              assemble = assemble + "2";
              break;
            case 30:
              assemble = assemble + "3";
              break;
            case 31:
              assemble = assemble + "4";
              break;
            case 32:
              assemble = assemble + "5";
              break;
            case 33:
              assemble = assemble + "6";
              break;
            case 34:
              assemble = assemble + "7";
              break;
            case 35:
              assemble = assemble + "8";
              break;
            case 36:
              assemble = assemble + "9";
              break;
          }

        }
        System.out.println(assemble);
        if (assemble.equalsIgnoreCase(pass)) {
          System.out.println("Password is: " + assemble);
          break; //replace this with: return assemble;
        } else {
          assemble = "";
        }
      }


    }
    return "";
  }
}

但是,当我 运行 程序时,密码 AAA 似乎从来没有用过,但 998 可以用,究竟是什么问题?

你的主要问题似乎是在正确的点打破循环。尝试使用标签来打破循环并解决此问题。在此处查找有关标签的更多信息:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

class BreakWithLabelDemo {
public static void main(String[] args) {

    int[][] arrayOfInts = { 
        { 32, 87, 3, 589 },
        { 12, 1076, 2000, 8 },
        { 622, 127, 77, 955 }
    };
    int searchfor = 12;

    int i;
    int j = 0;
    boolean foundIt = false;

search:
    for (i = 0; i < arrayOfInts.length; i++) {
        for (j = 0; j < arrayOfInts[i].length;
             j++) {
            if (arrayOfInts[i][j] == searchfor) {
                foundIt = true;
                break search;
            }
        }
    }

    if (foundIt) {
        System.out.println("Found " + searchfor + " at " + i + ", " + j);
    } else {
        System.out.println(searchfor + " not in the array");
    }
 }}

如果找到匹配,我认为你应该使用 return 语句 -

if (assemble.equalsIgnoreCase(pass)) {
    System.out.println("Password is: " + assemble);
    return assemble; // This is missing
}