2 回答

TA贡献1982条经验 获得超2个赞
public class BruteForce{
public static String password = "CBA";
public static Character[] characters = {'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', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '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 static Integer count = 0;
public static void main(String[] args) {
int deep = characters.length;//password deep,default value is from one to characters.length
rmark:
for (int i = 1; i <= deep; i++) {
for (int j = 0; j < characters.length; j++) {
if(test(i,characters[j].toString())) {
break rmark;
}
}
}
}
public static boolean test(int deep,String parent) {
if(deep <= 1) {
count++;
System.out.println(parent);
if(parent.equals(password)) {
System.out.println("after generating "+count+" strings,we find the password!");
return true;
}
return false;
}else {
for (int j = 0; j < characters.length; j++) {
if(test(deep-1,parent+characters[j].toString())) {
return true;
}
}
}
return false;
}
}

TA贡献2041条经验 获得超4个赞
只需开始以 x 为单位计数,其中 x 是您拥有的字符数。例如,如果您只关心数字,则可以使用常规的以10为基数的系统。以这种方式看待它是微不足道的,像50045这样的东西永远不会在5之前出现。
这样做非常简单,只需取一个开头为0的数组,然后每次需要新密码时,将第一个元素增加一个。如果它超过了您拥有的字符数,只需将其设置为零,然后将一个添加到下一个字符(如果是最后一个,则推送一个新元素)。
你可以更简单一点,只需使用一个简单的长整型(或者BigInteger表示更大的数字,长整型对于你的设置来说不能包含超过10个字符),然后从中获取字符,只需递归地取数字的模数和你正在处理的基数,然后除以基数。这看起来像这样:
for (long i = 0; i < maxNum; i++) {
long temp = i;
String pass = ""; // Use a StringBuilder here when actually attempting this
// This would eat your memory faster than 6 chrome tabs
do {
pass += charset[temp % base];
temp /= base;
} while (temp > 0);
}
添加回答
举报