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;
         }
}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Noob_Programmer
  • 111
  • 5
  • 14
  • 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. – Hot Licks Jan 14 '14 at 03:00

2 Answers2

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.

Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
  • 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? – Noob_Programmer Jan 14 '14 at 02:57
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?

ejbs
  • 390
  • 1
  • 9
  • 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! – Noob_Programmer Jan 14 '14 at 11:52