首页 > 解决方案 > 如何修复我的代码以破解字母、数字和特殊字符密码

问题描述

我正在做一个科学项目,我设置了一个程序来破解代码并显示计算机需要多长时间才能找到它,但它只适用于数字。我希望代码能够破解混合数量的密码任何顺序的特殊字符、字母和数字。我有点难过并且很长时间以来一直很沮丧,所以任何可以编辑并向我发送修改后的代码(或向我展示我做错了什么并提供解决方案)的人将不胜感激。提前致谢!

import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class consolePrint {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Type in a password!\n");
        //guess cannot be resolved to a variable??
        //prompt for pin,new line 
        // Commented out old input code  String password = 
        System.console().readLine(); //Read input to password string

        Scanner sc = new Scanner (System.in);
        String password= sc.nextLine();

        /*    Can only run 1- {insert set number} 
           -Protect input from bad data (ABC, etc) non-digits and or > 9 digits 
        [Prevent]
           -Master loop to keep request new input after reach run and clear the 
        screen *hint loop starts before 'set pin' prompt
         */

        long startTime = System.nanoTime(); // get time just before loop starts in nanoseconds to calculate speed
        for(int guess=0;guess<=1000000000;guess++) //start guess at 0 run until less than or equal 10,000 and add one each time to guess [9 digits]
        { 
            //System.out.println("Guess#");
            //System.out.println(guess);
            if (password.equals(Integer.toString(guess)))
            { 
                System.out.print("Nice! found in ");
                System.out.print((System.nanoTime()-startTime)/1000000000.0); //start time in nanoseconds subtracted from end and converted to seconds
                System.out.println(" seconds");
                break;
            }
            else
            {
                //System.out.println(" Sorry, wrong pin!");
            }
        }


    }
}

标签: java

解决方案


看起来很简单,但你的 for 循环只产生数字猜测。您不能期望在不生成具有混合模式的估计的情况下猜测具有混合模式的密码。你的猜测必须包括特殊字符和字母,你需要想办法产生这样的猜测。

做到这一点的一种方法是手头上有一系列可能的字符。首先将您猜测的每个字符设置为数组中第一个可能的字符。接下来,您应该通过跨越字符数组来更改每个字符。当然,猜测的数量和所需的时间将远远超过当前。


推荐阅读