第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

提高 ArrayList 排列的效率

提高 ArrayList 排列的效率

慕的地8271018 2022-07-27 16:07:21
作為一個小項目,我正在制作一個“密碼”破解程序,它只是暴力破解字母、數(shù)字和符號的所有排列,直到猜出密碼。當(dāng)然,這是非常低效的,我正在尋找讓它更快一點的方法。我的想法是讓排列按它們的大小順序發(fā)生。所以現(xiàn)在,它將從第一個字符開始,ArrayList并不斷添加下一個字符,猜測越來越大。所以例如..具有ArrayListvalues (A, B, C, D),我當(dāng)前的程序?qū)?chuàng)建如下排列:(A), (A, B), (A, B, C), (A, B, C, D), (A, B, D), (A, B, D, C)但更有效的方法(因為大多數(shù)密碼不是 60 多個字符長),將是通過這樣的排列(A), (B), (C), (D), (A, B), (A, C), (A, D), (B, A),(B, C)等等這是我當(dāng)前的程序的樣子:import java.util.ArrayList;import java.util.Arrays;public class BruteForce{    public static String password = "rand1";    public static void main(String[] args) {        ArrayList<Character> characters = new ArrayList<>(Arrays.asList('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', '_', '-', '!', '$'));        initialPermutations(characters);    }    public static void initialPermutations(ArrayList<Character> characters) {        ArrayList<Character> sub = new ArrayList<>();        finalPermutations(sub, characters);    }    public static void finalPermutations(ArrayList<Character> sub, ArrayList<Character> a) {        int L = a.size();        char[] cs = new char[sub.size()];        for(int i = 0; i < cs.length; i++) {            cs[i] = sub.get(i);        }        String output = new String(cs);        if(output.equals(password)) {            System.out.println("The password is " + output);            System.exit(-1);         }}關(guān)于如何將其改進為更“有效”(實際上不是更有效,只是對較短的密碼更有用)方法的任何想法?
查看完整描述

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;

    }

}


查看完整回答
反對 回復(fù) 2022-07-27
?
梵蒂岡之花

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);

}


查看完整回答
反對 回復(fù) 2022-07-27
  • 2 回答
  • 0 關(guān)注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號