Java数组绑定异常

时间:2015-10-08 11:43:39

标签: java

我正在尝试为密码安全制作一个程序,但很多时候我得到了Java Array Index Out of Bounds Exception ...我试图修复,但没有,这是我的代码:

import java.util.Arrays;

public class BruteForce {
    public static void main(String[] args) {

        String password = "aaaa";
        char[] charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
        BruteForce bf = new BruteForce(charset, 1);

        String attempt = bf.toString();
        while (true) {
            if (attempt.equals(password)) {
                System.out.println("Password Found: " + attempt); // low security
                break;
            }
            attempt = bf.toString();
            System.out.println(attempt);
            bf.increment();
        }
    }

    private char[] cs;
    private char[] cg;

    public BruteForce(char[] characterSet, int guessLength) {
        cs = characterSet;
        cg = new char[guessLength];
        Arrays.fill(cg, cs[0]);
    }

    public void increment() {
        int index = cg.length - 1;
        while (index >= 0) {
            if (cg[index] == cs[cs.length - 1]) {
                if (index == 0) {
                    cg = new char[cg.length + 1];
                    Arrays.fill(cg, cs[0]);
                    break;
                } else {
                    cg[index] = cs[0];
                    index--;
                }
            } else {
                cg[index] = cs[Arrays.binarySearch(cs, cg[index]) + 1];
                break;
            }
        }
    }

    @Override
    public String toString() {
        return String.valueOf(cg);
    }
}

当我尝试添加特殊字符时:

import java.util.Arrays;

public class BruteForce 
{
public static void main(String[] args) 
{

String password = "aaaa";
char[] charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.@#".toCharArray();
BruteForce bf = new BruteForce(charset, 1);

String attempt = bf.toString();
while (true) 
{
if (attempt.equals(password)) 
{
System.out.println("Password Found: " + attempt); // low security
break;
}
attempt = bf.toString();
System.out.println(attempt);
bf.increment();
}
}
private char[] cs;
private char[] cg;

public BruteForce(char[] characterSet, int guessLength) 
{
cs = characterSet;
cg = new char[guessLength];
Arrays.fill(cg, cs[0]);
}

public void increment() 
{
int index = cg.length - 1;
while(index >= 0) 
{
if (cg[index] == cs[cs.length-1]) 
{
if (index == 0) 
{
cg = new char[cg.length+1];
Arrays.fill(cg, cs[0]);
break;
}
else 
{
cg[index] = cs[0];
index--;
}
} 
else 
{
cg[index] = cs[Arrays.binarySearch(cs, cg[index]) + 1];
break;
}
}
}

@Override
public String toString() 
{
return String.valueOf(cg);
}
}

我得到这样的东西:

  

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:-65

1 个答案:

答案 0 :(得分:0)

之间的区别
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.@#"

是第一种情况,字符按升序排列(java中的字符有值)。

Arrays.binarySearch要求数组按升序排列。

如果您使用

"#.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

代替(或在Arrays.sort构造函数中使用BruteForce)它应该有用。

始终记得发布您正在使用的完全代码!