2 回答

TA貢獻1804條經(jīng)驗 獲得超8個贊
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貢獻1900條經(jīng)驗 獲得超5個贊
只需開始以 x 為底數(shù),其中 x 是您擁有的字符數(shù)。例如,如果您只關(guān)心數(shù)字,您將使用常規(guī)的 base 10 系統(tǒng)。以這種方式看待它是非常微不足道的,像 50045 這樣的東西永遠不會出現(xiàn)在 5 之前。
這樣做很簡單,只需取一個開頭包含 0 的數(shù)組,然后每次需要新密碼時,將第一個元素加一。如果它超過了您擁有的字符數(shù)量,只需將其設(shè)置為零并將一個添加到下一個(如果是最后一個,則推送一個新元素)。
您可以比這更簡單一點,只需使用一個簡單的 long (或 BigInteger 用于更大的數(shù)字,long 將不能包含超過 10 個字符用于您的設(shè)置),然后從中獲取字符,只需遞歸取數(shù)字和您正在使用的基數(shù)的模數(shù),然后除以基數(shù)。這看起來像這樣:
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);
}
添加回答
舉報