Java置换结果显示在jlabel

时间:2017-10-29 21:03:42

标签: java

有没有办法在jlabel中显示置换结果?

    int tests[] = permute(inputBits, keyBits, false);

我也尝试显示测试数组,但它是二进制的。

label.setText(Arrays.toString(tests));

有没有办法显示从permute到jlabel / jtextfield的结果?

Permute方法:基本上该程序用于SingleDes。用户输入16十六进制文本和14十六进制密钥。 Permute显示DES轮密钥,左右值和结束值。

private static int[] permute(int[] inputBits, int[] keyBits, boolean isDecrypt) {
        // Initial permutation step takes input bits and permutes into the
        // newBits array
        int newBits[] = new int[inputBits.length];
        for(int i=0 ; i < inputBits.length ; i++) {
            newBits[i] = inputBits[IP[i]-1];
        }

        // 16 rounds will start here
        // L and R arrays are created to store the Left and Right halves of the
        // subkey respectively
        int L[] = new int[32];
        int R[] = new int[32];
        int i;

        // Permuted Choice 1 is done here
        for(i=0 ; i < 28 ; i++) {
            C[i] = keyBits[PC1[i]-1];
        }
        for( ; i < 56 ; i++) {
            D[i-28] = keyBits[PC1[i]-1];
        }

        // After PC1 the first L and R are ready to be used and hence looping
        // can start once L and R are initialized
        System.arraycopy(newBits, 0, L, 0, 32);
        System.arraycopy(newBits, 32, R, 0, 32);
        System.out.print("\nL0 = ");
        displayBits(L);
        System.out.print("R0 = ");
        displayBits(R);
        for(int n=0 ; n < 16 ; n++) {
            System.out.println("\n-------------");
            System.out.println("Round " + (n+1) + ":");
            // newR is the new R half generated by the Fiestel function. If it
            // is encrpytion then the KS method is called to generate the
            // subkey otherwise the stored subkeys are used in reverse order
            // for decryption.
            int newR[] = new int[0];
            if(isDecrypt) {
                newR = fiestel(R, subkey[15-n]);
                System.out.print("Round key = ");
                displayBits(subkey[15-n]);
            } else {
                newR = fiestel(R, KS(n, keyBits));
                System.out.print("Round key = ");
                displayBits(subkey[n]);
            }
            // xor-ing the L and new R gives the new L value. new L is stored
            // in R and new R is stored in L, thus exchanging R and L for the
            // next round.
            int newL[] = xor(L, newR);
            L = R;
            R = newL;
            System.out.print("L = ");
            displayBits(L);
            System.out.print("R = ");
            displayBits(R);
        }

        // R and L has the two halves of the output before applying the final
        // permutation. This is called the "Preoutput".
        int output[] = new int[64];
        System.arraycopy(R, 0, output, 0, 32);
        System.arraycopy(L, 0, output, 32, 32);
        int finalOutput[] = new int[64];
        // Applying FP table to the preoutput, we get the final output:
        // Encryption => final output is ciphertext
        // Decryption => final output is plaintext
        for(i=0 ; i < 64 ; i++) {
            finalOutput[i] = output[FP[i]-1];
        }

        // Since the final output is stored as an int array of bits, we convert
        // it into a hex string:
        String hex = new String();
        for(i=0 ; i < 16 ; i++) {
            String bin = new String();
            for(int j=0 ; j < 4 ; j++) {
                bin += finalOutput[(4*i)+j];
            }
            int decimal = Integer.parseInt(bin, 2);
            hex += Integer.toHexString(decimal);
        }
        if(isDecrypt) {
            System.out.print("Decrypted text: ");

        } else {
            System.out.print("Encrypted text: ");
        }
        System.out.println(hex.toUpperCase());
        return finalOutput;
    }

0 个答案:

没有答案