为了账号安全,请及时绑定邮箱和手机立即绑定

提高数组列表排列的效率

提高数组列表排列的效率

慕码人8056858 2022-08-03 15:42:31
作为一个小项目,我正在制作一个“密码”破解程序,它只是暴力破解字母表,数字和符号的所有排列,直到密码被猜到。当然,这是非常低效的,我正在寻找使它更快一点的方法。我的想法是让排列按照它们的大小顺序发生。所以现在,它将从中的第一个字符开始,并不断添加下一个字符,猜测变得越来越大,越来越大。例如..ArrayList具有值的 A,我当前的程序将创建如下排列:ArrayList(A, B, C, D)(A), , , , ,(A, B)(A, B, C)(A, B, C, D)(A, B, D)(A, B, D, C)但是一种更有效的方法(因为大多数密码的长度不是60多个字符),那就是像这样进行排列(A)、、、、、、、等(B)(C)(D)(A, B)(A, C)(A, D)(B, A)(B, C)这是我当前的程序的样子: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);        }        if (L == 0) {            System.out.println(output);            }        }    }}关于如何将其改进为更“有效”(实际上不是更有效,只是对于更短的密码更有用)方法的任何想法?
查看完整描述

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;

    }

}


查看完整回答
反对 回复 2022-08-03
?
缥缈止盈

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

}


查看完整回答
反对 回复 2022-08-03
  • 2 回答
  • 0 关注
  • 107 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号