1

在 java 中使用 des 进行一些加密。我有一堆方法以及一些 junit 测试用例。我遇到问题的方法接收一个字节数组和一个 int,并在该位置(int)翻转该位。因此,就像使用总共 64 位的 8 字节字节数组一样,它接收十六进制字节数组 [ ad 12 45 67 c3 65 3a 66 ] 将其转换为 64 位长的二进制字符串并翻转任何位置。此方法通过了 junit 中的测试用例(已提供给我),但现在我尝试对程序执行其他操作,但它通常不会让我调用翻转。

这是我得到的代码和错误。在翻转的第一个 for 循环中,人们建议我将 <= 更改为仅 < ,但这样做会导致我的 junit 测试失败。

我的目标是一次一点地浏览给定的明文并翻转它以观察给定密文的变化。这就是 diff 方法的用途(计算 2 字节数组之间的差异)。这一切都是在 main 的那个小的 for 循环中完成的。

public static void main(String[] args){

        //take original plain text and get the ct to compare all other ct's to
        String plaintxt = "Coolbro!";
        byte [] ptAr = getBytes(asciiToHex(plaintxt));
        byte [] ctFinal = encrypt(ptAr);
        byte [] ptCopy;
        byte [] newCt;
        int differences =0;

        for ( int j = 63; j >= 0; j--){

            ptCopy = flip(ptAr, 2);
            newCt = encrypt(ptCopy);
            differences = diff(ctFinal,newCt);
            System.out.println(differences);

        }


    }

    public static int diff( byte [] a, byte [] b){
    int diff = 0;
    String byteAr1;
    String byteAr2;
    char A1 [];
    char A2 [];

    byteAr1 = hexToBin(a);
    byteAr2 = hexToBin(b);

    A1 = byteAr1.toCharArray();
    A2 = byteAr2.toCharArray();

    for( int i = 0; i < A1.length; i++){
        if(A1[i] != A2[i]){
            diff++;
        }
    }
    return diff;
}

    public static byte [] flip(byte [] a, int position){

        byte[] copy = a;
        String temp = "";
        String tempf = "";
        for(int i = 0; i <= a.length; i++){
            temp = temp + String.format("%8s", Integer.toBinaryString(a[i])).replace(' ', '0');
        }
        if(temp.charAt(position) == '1'){
            for(int i = 0; i < temp.length(); i++){
                if (i == position){
                    tempf += "0";
                }
                else{
                    tempf += temp.charAt(i);
                }
            }
        }
        else{
            for(int i = 0; i < temp.length(); i++){
                if (i == position){
                    tempf += "1";
                }
                else{
                    tempf += temp.charAt(i);
                }
            }
        }
        temp = Integer.toHexString(Integer.parseInt(tempf, 2));
        byte [] fin = temp.getBytes();


        return fin;

以下是这对我产生的错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at AvalancheUtilities.flip(AvalancheUtilities.java:55)
at AvalancheUtilities.main(AvalancheUtilities.java:18)

就像我上面所说的,如果我将 <= 更改为只是一个 < 那么我会得到这些错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "0110001101101111011011110110110001100010011100100110111100100001"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at AvalancheUtilities.flip(AvalancheUtilities.java:77)
at AvalancheUtilities.main(AvalancheUtilities.java:18)

我真的不知道如何处理这两种情况。最好我想坚持 <= 因为它通过了测试用例。

感谢您的帮助,我知道这很多!

4

1 回答 1

0

您的“人”是正确的,您想将 <= 更改为 <。查看您的异常,您的数组中只有 8 个项目,这意味着您只能从 0 到 7 进行索引。最后一个索引将始终比数组的长度小 1。

对于另一个例外,问题是您的数字太大。请参阅Integer.MAX_VALUE

将其切换为:

temp = Long.toHexString(Long.parseLong(tempf, 2));
于 2013-03-01T03:39:58.840 回答