我有一个蛮力算法,但从未完全理解它。我对正在发生的一些事情有一个模糊的把握,但每次我试图准确地跟踪发生的事情时,我都会迷失方向(例如,index
变量有点令人困惑)。也欢迎任何有关如何使算法更高效的提议。
注意- 我已经有了算法,它可以编译和工作。请不要指责我试图将其用于恶意目的,因为我没有将其用于此目的,而且我从未打算这样做。我只是想知道它是如何工作的。
public class BruteForceTest
{
public String username = new String();
public static String password = "ZZZZZ";
public static char[] charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private static char[] currentGuess = new char[1];
public static void bruteForce()
{
String attempt = new String();
Date start = new Date();
while (true)
{
if (attempt.equals(password))
{
Date end = new Date();
System.out.println("Password: " + attempt + "\nTotal time to crack: " + ((end.getTime() - start.getTime()) / 1000) + " seconds." + "\n");
break;
}
attempt = in.toString();
// System.out.println("Tried: " + attempt);
in.increment();
}
}
public BruteForceTest()
{
Arrays.fill(currentGuess, charset[0]);
}
public void increment()
{
int index = currentGuess.length - 1;
while (index >= 0)
{
if (currentGuess[index] == charset[charset.length - 1])
{
if (index == 0)
{
currentGuess = new char[currentGuess.length + 1];
Arrays.fill(currentGuess, charset[0]);
break;
}
else
{
currentGuess[index] = charset[0];
index--;
}
}
else
{
currentGuess[index] = charset[Arrays.binarySearch(charset, currentGuess[index]) + 1];
break;
}
}
}
public String toString()
{
return String.valueOf(currentGuess);
}
}