Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的暴力密码生成器(Java)出错_Java_Encryption_Brute Force - Fatal编程技术网

我的暴力密码生成器(Java)出错

我的暴力密码生成器(Java)出错,java,encryption,brute-force,Java,Encryption,Brute Force,这是一个类的赋值,基本上是创建一个可以“猜测”密码的方法,然后将密码发送给另一个类,该类将解密文件。我已经完成了最难的部分 import sussex.edu.Cryptography; public class Final { public static void main(String[] args) throws Exception{ //decrypter(); Cryptography crypto = new Cryptography();

这是一个类的赋值,基本上是创建一个可以“猜测”密码的方法,然后将密码发送给另一个类,该类将解密文件。我已经完成了最难的部分

import sussex.edu.Cryptography;

public class Final {

    public static void main(String[] args) throws Exception{
        //decrypter();
        Cryptography crypto = new Cryptography();

        String encrypted = "SourceFile_encrypted.txt";
        String decrypted = "SourceFile_decrypted.txt";
        String password = new String();

        crypto.setPassword(password);
        if(crypto.isPasswordValid()){
            System.out.println("Found password:" + password);
            crypto.decryptFile(encrypted, decrypted);
        }
        else{
            //Keep trying...
        }
    }
    public static String decrypter(){
        char array[] = new char[5];
        Cryptography crypto = new Cryptography();
        String password = new String();

        for (char c0 = 'A'; c0 <= 'Z'; c0++) {
            array[0] = c0;
            for (char c1 = 'A'; c1 <= 'Z'; c1++) {
                array[1] = c1;
                for (char c2 = 'A'; c2 <= 'Z'; c2++) {
                    array[2] = c2;
                    for (char c3 = 'A'; c3 <= 'Z'; c3++) {
                        array[3] = c3;
                        for (char c4 = 'A'; c4 <= 'Z'; c4++){
                            array[4] = c4;
                            String s = new String(array);
                            password = s;
                            System.out.println(password);
                            crypto.setPassword(password);
                            if(crypto.isPasswordValid()){
                                break;
                            }
                        }
                    }
                }
            }
        }
        return password;
    }
}
导入sussex.edu.Cryptography;
公开课决赛{
公共静态void main(字符串[]args)引发异常{
//解密程序();
加密加密=新加密();
String encrypted=“SourceFile\u encrypted.txt”;
String decrypted=“SourceFile\u decrypted.txt”;
字符串密码=新字符串();
加密设置密码(密码);
if(crypto.isPasswordValid()){
System.out.println(“找到密码:+密码”);
加密解密文件(加密、解密);
}
否则{
//继续努力。。。
}
}
公共静态字符串解密程序(){
字符数组[]=新字符[5];
加密加密=新加密();
字符串密码=新字符串();

对于(char c0='A';c0可能您的isPasswordValid()方法将字符串与==进行比较,而不是与equals()进行比较

这可以解释为什么这个比较在与“OLLEH”比较时返回true,而在与具有相同值但使用
new
创建的字符串比较时返回false


尝试用
password=s.intern()替换
password=s;

在decryptor方法中,您创建了一个本地cryptor变量,但从未为该实例实际将密码设置为OLLEH。不确定Cryptography类如何工作,但这可能是您的问题吗?我如何解决此问题?我没有访问isPasswordValid()的权限方法。它现在可以工作了!只是,循环没有停止,我不知道如何停止循环字符的for循环。(我已经尝试添加了
break;
break只停止内部for。要中断外部for,可以使用标签。