2014-01-14 83 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。但我仍然不知道如何去解决这个错误。我如何组合这些零件? –