指定 while 条件会导致程序 return 什么都不做
Specifying while condition causes program to return nothing
我目前正在研究一种暴力破解密码的方法,因为我想尝试一下并做一些新的事情。我在下面提供了我正在研究的方法,但首先是我正在尝试做的事情。最初,我为密码生成一个可能值的散列值,并将其与我的 "sample.txt" 文件中包含散列密码列表的散列值进行比较。所以基本上我的目标是从该外部文件读取散列密码值并将其与所有可能的 3/4 散列密码值进行比较。
当我运行程序时,我不小心将"BruteForce"方法中的while语句的条件保持为true并且它无休止地运行,但是当我将条件设置为将生成的密码值保持为 3 和 4 的长度,程序突然终止我不知道为什么,我尝试调试程序以查看哪里出了问题,但我无法推断出任何东西。
这是我的主要方法和 "bruteforce" 方法中的内容:
public TestClass(char[] characterSet, int guessLength) {
cs = characterSet;
cg = new char[guessLength];
Arrays.fill(cg, cs[0]);
}
public static void bruteForce(String username, String hashed_pw) {
//username is the username from the input
//hashed_pw is the hashed value of password
String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
char[] charset = chars.toCharArray();
TestClass bf = new TestClass(charset, 1); //random generation of possible pw value
String attempt = bf.toString();
while ((attempt.length() == 3) || (attempt.length() == 4)) {
String hashed_input = doHash(attempt); //hash the possible pw value
System.out.println("");
System.out.println("Attempt result is: " + attempt);
System.out.println("Hashed of attempt: " + hashed_input);
System.out.println("Hashed Password is : " + hashed_pw);
System.out.println("");
if (hashed_input.equals(hashed_pw)) {
System.out.println("Password Found: " + attempt);
System.out.println(username + "'s password is: " + attempt);
break;
} else {
attempt = bf.toString();
bf.increment();
}
// attempt = bf.toString();
// System.out.println("" + attempt);
// bf.increment();
// return attempt;
}
// return attempt;
}
public char[] cs;
public char[] cg;
public void increment() {
int index = cg.length - 1;
while (index >= 0) {
if (cg[index] == cs[cs.length - 1]) {
if (index == 0) {
cg = new char[cg.length + 1];
Arrays.fill(cg, cs[0]);
break;
} else {
cg[index] = cs[0];
index--;
}
} else {
cg[index] = cs[Arrays.binarySearch(cs, cg[index]) + 1];
break;
}
}
}
@Override
public String toString() {
return String.valueOf(cg);
}
当 运行 带有 bruteForce(s[0],s[1]) 的代码时,它不提供任何输出,只给出 BUILD SUCCESSFUL 消息。
s[0] is the username of the user I'm trying to deduce their password
s[1] is the hashed password I read from an external file
我在bruteForce方法中将s[1]值与hashed_input值进行比较,条件是我的用户可能生成的密码只有3或4个字符的长度
TestClass bf = new TestClass(charset, 3);
将解决您的问题。
我目前正在研究一种暴力破解密码的方法,因为我想尝试一下并做一些新的事情。我在下面提供了我正在研究的方法,但首先是我正在尝试做的事情。最初,我为密码生成一个可能值的散列值,并将其与我的 "sample.txt" 文件中包含散列密码列表的散列值进行比较。所以基本上我的目标是从该外部文件读取散列密码值并将其与所有可能的 3/4 散列密码值进行比较。
当我运行程序时,我不小心将"BruteForce"方法中的while语句的条件保持为true并且它无休止地运行,但是当我将条件设置为将生成的密码值保持为 3 和 4 的长度,程序突然终止我不知道为什么,我尝试调试程序以查看哪里出了问题,但我无法推断出任何东西。
这是我的主要方法和 "bruteforce" 方法中的内容:
public TestClass(char[] characterSet, int guessLength) {
cs = characterSet;
cg = new char[guessLength];
Arrays.fill(cg, cs[0]);
}
public static void bruteForce(String username, String hashed_pw) {
//username is the username from the input
//hashed_pw is the hashed value of password
String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
char[] charset = chars.toCharArray();
TestClass bf = new TestClass(charset, 1); //random generation of possible pw value
String attempt = bf.toString();
while ((attempt.length() == 3) || (attempt.length() == 4)) {
String hashed_input = doHash(attempt); //hash the possible pw value
System.out.println("");
System.out.println("Attempt result is: " + attempt);
System.out.println("Hashed of attempt: " + hashed_input);
System.out.println("Hashed Password is : " + hashed_pw);
System.out.println("");
if (hashed_input.equals(hashed_pw)) {
System.out.println("Password Found: " + attempt);
System.out.println(username + "'s password is: " + attempt);
break;
} else {
attempt = bf.toString();
bf.increment();
}
// attempt = bf.toString();
// System.out.println("" + attempt);
// bf.increment();
// return attempt;
}
// return attempt;
}
public char[] cs;
public char[] cg;
public void increment() {
int index = cg.length - 1;
while (index >= 0) {
if (cg[index] == cs[cs.length - 1]) {
if (index == 0) {
cg = new char[cg.length + 1];
Arrays.fill(cg, cs[0]);
break;
} else {
cg[index] = cs[0];
index--;
}
} else {
cg[index] = cs[Arrays.binarySearch(cs, cg[index]) + 1];
break;
}
}
}
@Override
public String toString() {
return String.valueOf(cg);
}
当 运行 带有 bruteForce(s[0],s[1]) 的代码时,它不提供任何输出,只给出 BUILD SUCCESSFUL 消息。
s[0] is the username of the user I'm trying to deduce their password
s[1] is the hashed password I read from an external file
我在bruteForce方法中将s[1]值与hashed_input值进行比较,条件是我的用户可能生成的密码只有3或4个字符的长度
TestClass bf = new TestClass(charset, 3);
将解决您的问题。