0

I need some help with my program here. Can anyone help me out with this?

Thanks!

Every time I run my code, I get the following output:

enter image description here enter image description here enter image description here

But I want the output to be like this in one box instead of many: enter image description here

Code:

public class myDesCbc2 {

    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

JFrame frame = null;
JFileChooser fChoose = new JFileChooser(System.getProperty("user.home"));
int returnVal = fChoose.showOpenDialog(frame);
File myFile = fChoose.getSelectedFile();

//Read file and store to String line
FileInputStream fis = new FileInputStream(myFile);
BufferedReader stream = new BufferedReader(new InputStreamReader(fis, "ISO-8859-1"));
String file;
while ((file = stream.readLine()) != null) {

    JOptionPane.showOptionDialog(
            null, "Generating a 56-bit DES key...", "Processing...", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);
    // Create an 8-byte initialization vector
    SecureRandom sr = new SecureRandom();
    byte[] iv = new byte[8];
    sr.nextBytes(iv);
    IvParameterSpec IV = new IvParameterSpec(iv);

    // Create a 56-bit DES key
    KeyGenerator kg = KeyGenerator.getInstance("DES");

    // Initialize with keysize
    kg.init(56);
    Key mykey = kg.generateKey();

    JOptionPane.showOptionDialog(
            null, "Your key has been generated!", "Processing...", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);

    // Create a cipher object and use the generated key to initialize it
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

    cipher.init(Cipher.ENCRYPT_MODE, mykey, IV);

    byte[] plaintext = file.getBytes("UTF8");

    // Encrypt the text
    byte[] ciphertext = cipher.doFinal(plaintext);

   JOptionPane.showMessageDialog(null,"\n\nCiphertext: ");
    for (int i = 0; i < ciphertext.length; i++) {

        if (chkEight(i)) {
            System.out.print("\n");
        }
        JOptionPane.showMessageDialog(null,ciphertext[i] + " ");
    }
}
}
}

chkEight code:

public class chkEight {
      public static Boolean chkEight (int num) {
         int num1, rem;
         num1 = num % 8;
         if(num1== 0) {
             return true;
         }
         else
         {
             return false;
         }
}
}
1
  • 2
    Yeah, programs often give you the wrong output. Or at least what you think is "wrong" -- in virtually all cases they are doing what you told them to do. Commented Jan 14, 2014 at 3:00

2 Answers 2

2

Your error is in this part:

JOptionPane.showMessageDialog(null,"\n\nCiphertext: ");
for (int i = 0; i < ciphertext.length; i++) {

    if (chkEight(i)) {
        System.out.print("\n");
    }
    JOptionPane.showMessageDialog(null,ciphertext[i] + " ");
}

You want to take all those ciphertext[i] parts and somehow combine them. Then you can display a single MessageDialog outside of your loop. That will achieve the desired result.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok thanks for pointing that out @Jean-Bernard Pellerin. But I still dont know how to go about fixing the error. How do I combine the parts?
1

To expand on Jean-Bernard's answer:

String concatenation is done like this in Java:

String s1 = "hello";
String s2 = "world";
String s3 = s1+" "+s2; // "hello world"

Therefore what you want to do is concatenate all of the strings (with a loop) before you show the dialog box.

Which you would do like this:

String collection = "";
for(int i  = 0; i < cihpertext.length; i++) {
    collection += " "+ciphertext[i];
    if(chkEight(i)) [
        collection += "\n"
    }
}
JOptionPane.showMessageDialog(null, collection);

EDIT: To clarify what your mistake is:

JOptionPane.showMessageDialog(null,"\n\nCiphertext: ");
for (int i = 0; i < ciphertext.length; i++) {

    if (chkEight(i)) {
        System.out.print("\n");
    }
    JOptionPane.showMessageDialog(null,ciphertext[i] + " ");
}

In this code you:

  1. Try to print a newline to the terminal if chkEight(i) returns true; this won't append anything to the string.

  2. Then you call showMessageDialog for every iteration in the loop, showing the current ciphertext element plus a space.

Are you sure that you understand your own code?

1 Comment

Thank you very much for that clear explanation. Im still new at programming and the loop was done for me by a friend. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.