java - 传递字节数组;通过junit测试,但无法定期调用方法?

标签 java encryption for-loop error-handling arrays

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

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

我的目标是一次一点地浏览给定的明文并翻转它以观察给定密文的变化。这就是 diff 方法的用途(计算 2 个字节数组之间的差异)。这一切都是在主程序中的小 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)

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

感谢您提供的任何帮助,我知道这非常多!

最佳答案

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

对于另一个异常(exception),问题是您的数字太大。请参阅Integer.MAX_VALUE .

切换到:

temp = Long.toHexString(Long.parseLong(tempf, 2));

关于java - 传递字节数组;通过junit测试,但无法定期调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15149875/

相关文章:

sql-server - SQL Server CLR 内存分配

javascript - 在for循环中计算JSON中两个值之间的百分比

C - strcspn() 函数跳过数组中较长的字符串

java - EditText 如何占据布局中的所有剩余空间?

azure - Service Fabric - 可靠的字典加密模型

java - 获取没有时间的当前日期

c# - C#和Java中的加密方法

c++ - 基于范围的 For 循环

java - 在代码中使用函数时的 Eclipse 参数对话框/提示

java - 如何在Eclipse中选择一个单词的所有出现