【问题标题】:Recursion - Displaying wrong number of attempts递归 - 显示错误的尝试次数
【发布时间】:2021-07-01 01:14:44
【问题描述】:

所以代码工作正常,但是破解 5 个字母密码的尝试次数不正确。我试过修复东西,但它总是给我 3 位数。尝试的次数应该更高。 这是我的代码:

import java.util.Scanner;
import java.util.Random;

class Main {
    public static void main(String[] args) {
        // Letters for the random generated password
        // Variables
        String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        Random order = new Random();
        int PASSWORD = letters.length();
        // While statement to allow user to keep generating passwords
        while (true) {
            String password = "";
            Scanner input = new Scanner(System.in);
            // Print/menu
            System.out.println("Press 1 to generate a random password");
            // Takes user input
            int UserOption = input.nextInt();

            // If user input equals 1
            if (UserOption == 1) {
                // Generate a 5-character passwords from the letters in the String
                for (int i = 0; i < 5; i++) {
                    password = password + letters.charAt(order.nextInt(PASSWORD));
                }
                System.out.println(password);
                cracking(5, password, letters, 0, "");
            }
            // If user input is anything except 1
            else {
                // Print error
                System.out.println("Error");
            }
        }
    }

    //Method for cracking password
    private static int cracking(int length, String password, String characters, int tries, String tryPass) {
        System.out.println(length);
        if (length == 0) {
            System.out.println("It took " +  tries + " tries to crack the password");
            return 0;
        }

        for (int i = 0; i < characters.length(); i++) {
            if (password.charAt(length-1) == characters.charAt(i)) {
                tryPass = tryPass + characters.charAt(i);
                break;
            }
            tries++;
        }
        cracking((length-1), password, characters, tries, tryPass);
        return 0;
    }
}

【问题讨论】:

    标签: java recursion passwords


    【解决方案1】:

    您使用的字符集的长度是 62。letters 中的最后一个字符是 0,因此匹配 0 所需的尝试次数是 61 尝试次数。

    因此,即使随机生成的 5 个字母密码包含 00000(都是您字符集中的最后一个字符。),总尝试次数将是 61*5,即 305。所以输出永远不会大于这个值。

    因此,此代码永远不会返回远高于 3 位数字的值。 (我假设您期望 4 或 5 位数字)。此外,它可以返回的最大值是305

    如果您需要更高的尝试次数,请增加密码长度。

    【讨论】:

    • 这是有道理的,但在这种情况下,不是每个字母最多需要 62 次尝试,而 5 个字母总共需要 62^5 次组合吗?在破解密码之前,我需要进行哪些更改才能包含所有可能的组合?
    • @UO23 是的,有 62^5 种组合。但是对于每个位置,最多需要 62 次尝试才能找到正确的字符。因此,最大尝试次数为 = max tries per letter * number of letters,即 62*5(在当前代码中,tries 在字符匹配时不会增加,因此最大尝试次数为 61*5)。您能否解释一下为什么需要检查所有可能的组合(按字典顺序?)来破解密码?因为,这种方法会降低性能并且没有其他好处。
    • @UO23 如果可以在 62*5 步或 62^5 步中找到答案,请选择 62*5 步解决方案。
    猜你喜欢
    • 2021-07-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 2020-01-21
    • 1970-01-01
    相关资源
    最近更新 更多