- Home /
Brute Force Search Algorithm
Making a brute force search algorithm. A user enters a sequence of upper case letters into the text box and the script will find the password through the brute force method. My problem is that it works with strings of 1 or 2 letters, but goes into an infinite loop when I do anymore than 2 letters and I can't figure out why. Sorry for the messy code.
public Text text1;
public Text text2;
public InputField input;
public Button button;
private int tempIndex = 25;
private string[] charList;
private int numDigits = 1;
private List<string> curGuess = new List<string>();
private int numGuesses = 0;
private string actGuess;
private string password;
private int indexNum = 0;
private float tempfloat = 0.0f;
// Use this for initialization
void Start () {
charList = new string[] {"","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
}
public void bruteForce() {
password = input.text;
numGuesses = 0;
indexNum = 0;
tempIndex = 26;
numDigits = 1;
actGuess = "";
curGuess = new List<string> ();
curGuess.Add ("");
while (actGuess != password) {
indexNum += 1;
curGuess [numDigits - 1] = charList [((indexNum + 26) % 26)];
numGuesses += 1;
actGuess = string.Join ("", curGuess.ToArray());
if (actGuess != password) {
if (((indexNum + 26) % 26) == 0) {
if (indexNum == tempIndex) {
tempIndex = 26 ^ (numDigits + 1);
numDigits += 1;
curGuess.Add ("");
}
for (int i = 1; i <= curGuess.Count - 1; i++) {
tempfloat = Mathf.Floor(indexNum / (26 ^ i));
tempfloat += 26;
tempfloat = tempfloat % 26;
curGuess [numDigits - i - 1] = charList [(int)tempfloat];
}
}
}
}
Debug.Log (actGuess);
text1.text = "Password is " + actGuess;
text2.text = "Num Guesses " + numGuesses;
}
Comment