2014-01-14 4 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

을 장 버나드의 대답에 확장하려면 :

문자열 연결 자바에서 다음과 같이 수행됩니다

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을 지적 해 주셔서 감사합니다. 그러나 나는 아직도 오류를 고치는 방법을 모른다. 부품을 어떻게 결합합니까? –

관련 문제