2014-01-14 82 views
0

我需要一些幫助,我的程序在這裏。任何人都可以幫我解決這個問題嗎?程序給出錯誤輸入

謝謝!

每次我跑我的代碼,我得到以下的輸出:

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

但我想輸出是這樣在一個盒子裏,而不是多個: enter image description here

代碼:

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代碼:

public class chkEight { 
     public static Boolean chkEight (int num) { 
     int num1, rem; 
     num1 = num % 8; 
     if(num1== 0) { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
} 
} 
+1

是的,程序經常給你錯誤的輸出。或者至少你認爲是「錯誤的」 - 幾乎在所有情況下,他們都在做你告訴他們做的事情。 –

回答

0

爲了擴大對讓 - 伯納德的答案:

字符串連接在Java中這樣做:

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

因此,你要做的就是串聯所有的字符串(用循環)什麼之前你顯示對話框。

,你會做這樣的:

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

編輯:爲了澄清自己的錯誤是什麼:

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] + " "); 
} 

在此代碼:

  1. 嘗試打印如果chkEight(i)返回true,則換行到終端;這不會將任何內容追加到字符串中。

  2. 然後你在循環中每次迭代調用showMessageDialog,顯示當前的密文元素加上一個空格。

您確定您瞭解自己的代碼嗎?

+0

非常感謝您的明確解釋。我仍然是編程新手,這個循環是由朋友爲我完成的。再次感謝! –

1

你的錯誤是在這一部分:

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] + " "); 
} 

你想利用所有這些ciphertext[i]部分並以某種方式將它們結合起來。然後,您可以在循環之外顯示一個MessageDialog。這將達到預期的結果。

+0

好的,謝謝你指出@ Jean-Bernard Pellerin。但我仍然不知道如何去解決這個錯誤。我如何組合這些零件? –