Reputation: 111
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:
But I want the output to be like this in one box instead of many:
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;
}
}
}
Upvotes: 0
Views: 81
Reputation: 390
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:
Try to print a newline to the terminal if chkEight(i) returns true; this won't append anything to the string.
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?
Upvotes: 1
Reputation: 12670
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.
Upvotes: 2