2017-01-28 5 views
-2

を続け、 私は二つの問題があります。読むJavaですべてのフォルダとこのJava解読(AES128)が働く操作

  1. ファイルが破損しているときはいつでも、残りのファイルは復号化されていません。 (ファイルが10個あり、ファイル番号4が破損している場合、ファイル5-10は復号化されません)
  2. フォルダ内のすべてのフォルダを読み取るにはどうすればよいですか?
public class DecoderGui extends javax.swing.JPanel { 

/** 
* Creates new form DecoderGui 
*/ 

public static void main(String[] args) 
{ 
    Security.addProvider(new BouncyCastleProvider()); 
    JFrame frame = new JFrame(); 
    frame.add(new DecoderGui()); 
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    frame.setResizable(false); 
    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
}  

public DecoderGui() { 
    initComponents(); 
} 




@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    keyFile = new javax.swing.JTextField(); 
    jLabel1 = new javax.swing.JLabel(); 
    sourceDirectory = new javax.swing.JTextField(); 
    destinationDirectory = new javax.swing.JTextField(); 
    keyButton = new javax.swing.JButton(); 
    sourceButton = new javax.swing.JButton(); 
    destinationButton = new javax.swing.JButton(); 
    decryptButton = new javax.swing.JButton(); 
    exitButton = new javax.swing.JButton(); 
    message = new javax.swing.JLabel(); 

    jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N 
    jLabel1.setText("Schutzstaffel Decryption"); 

    destinationDirectory.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      destinationDirectoryActionPerformed(evt); 
     } 
    }); 

    keyButton.setText("Key File"); 
    keyButton.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      keyButtonActionPerformed(evt); 
     } 
    }); 

    sourceButton.setText("Browse"); 
    sourceButton.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      sourceButtonActionPerformed(evt); 
     } 
    }); 

    destinationButton.setText("Save"); 
    destinationButton.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      destinationButtonActionPerformed(evt); 
     } 
    }); 

    decryptButton.setText("Decrypt"); 
    decryptButton.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      decryptButtonActionPerformed(evt); 
     } 
    }); 

    exitButton.setText("Exit"); 
    exitButton.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      exitButtonActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
    this.setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(84, 84, 84) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addGroup(layout.createSequentialGroup() 
           .addGap(29, 29, 29) 
           .addComponent(decryptButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE) 
           .addGap(84, 84, 84) 
           .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)) 
          .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addGroup(layout.createSequentialGroup() 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
           .addComponent(keyFile, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE) 
           .addComponent(sourceDirectory) 
           .addComponent(destinationDirectory)) 
          .addGap(43, 43, 43) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
           .addComponent(keyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE) 
           .addComponent(destinationButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))) 
         .addComponent(sourceButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(145, 145, 145) 
        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE))) 
      .addContainerGap(99, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(20, 20, 20) 
      .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(31, 31, 31) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(keyFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(keyButton)) 
      .addGap(33, 33, 33) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(sourceDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(sourceButton)) 
      .addGap(34, 34, 34) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(destinationDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(destinationButton)) 
      .addGap(27, 27, 27) 
      .addComponent(message) 
      .addGap(18, 18, 18) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(decryptButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addContainerGap(82, Short.MAX_VALUE)) 
    ); 
}// </editor-fold>       

private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {           
    System.exit(0); 
}           

private void decryptButtonActionPerformed(java.awt.event.ActionEvent evt) {            
    try { 
     if(keyFile.getText() == null || keyFile.getText().equals("")) { 
      message.setText("Please select the key file"); 
     } 
     if(sourceDirectory.getText() == null || sourceDirectory.getText().equals("")) { 
      message.setText("Please select the source folder"); 
     } 
     if(destinationDirectory.getText() == null || destinationDirectory.getText().equals("")) { 
      message.setText("Please select the destination folder"); 
     } 

     Decoder decoder = new Decoder(); 

     File sourcedir = new File(sourceDirectory.getText()); 
     File destinationdir = new File(destinationDirectory.getText()); 

    // System.out.println(seeder); 

     String seeder = new String(decoder.getFileDataToStream(keyFile.getText())); 

     System.out.println(seeder); 
     File[] directoryListing = sourcedir.listFiles(); 
     if (directoryListing != null) { 
      for (File child : directoryListing) { 
       System.out.println(child.getCanonicalPath()); 
       byte[] inputstream = decoder.getFileDataToStream(child.getCanonicalPath()); 
       byte[] outputstream = decoder.decrypt(seeder, inputstream); 
       FileOutputStream fos = new FileOutputStream(destinationDirectory.getText()+File.separator+new String(Base64.decodeBase64(child.getName()))); 
       fos.write(outputstream); 
       fos.close(); 

      } 
      message.setText("Decryption done"); 
     } else { 
      message.setText("Error while Listing the Source Directory Files"); 
     } 
    } catch (IOException ex) { 
     Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (Exception ex) { 
     Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex); 
    } 

}            

private void keyButtonActionPerformed(java.awt.event.ActionEvent evt) {           
    JFileChooser chooser = new JFileChooser(); 
    chooser.setCurrentDirectory(new java.io.File(".")); 
    chooser.setDialogTitle("Choose Key File"); 
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 
    chooser.setAcceptAllFileFilterUsed(false); 

    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 
     try { 
      System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory()); 
      System.out.println("getSelectedFile() : " + chooser.getSelectedFile()); 
      keyFile.setText(chooser.getSelectedFile().getCanonicalPath()); 
     } catch (IOException ex) { 
      Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } else { 
     System.out.println("No Selection "); 
    } 
}           

private void sourceButtonActionPerformed(java.awt.event.ActionEvent evt) {            
    JFileChooser chooser = new JFileChooser(); 
    chooser.setCurrentDirectory(new java.io.File(".")); 
    chooser.setDialogTitle("Choose Source Folder"); 
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
    chooser.setAcceptAllFileFilterUsed(false); 

    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 
     try { 
      System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory()); 
      System.out.println("getSelectedFile() : " + chooser.getSelectedFile()); 
      sourceDirectory.setText(chooser.getSelectedFile().getCanonicalPath()); 
     } catch (IOException ex) { 
      Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } else { 
     System.out.println("No Selection "); 
    } 
}            

private void destinationButtonActionPerformed(java.awt.event.ActionEvent evt) {             
    JFileChooser chooser = new JFileChooser(); 
    chooser.setCurrentDirectory(new java.io.File(".")); 
    chooser.setDialogTitle("Choose Destination Folder"); 
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
    chooser.setAcceptAllFileFilterUsed(false); 

    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 
     try { 
      System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory()); 
      System.out.println("getSelectedFile() : " + chooser.getSelectedFile()); 
      destinationDirectory.setText(chooser.getSelectedFile().getCanonicalPath()); 
     } catch (IOException ex) { 
      Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } else { 
     System.out.println("No Selection "); 
    } 
}             

private void destinationDirectoryActionPerformed(java.awt.event.ActionEvent evt) {              
    // TODO add your handling code here: 
} 

答えて

0

問題1: あなたのコードは、forループを終了し、ファイルを復号化中にエラーが発生した、したがって、他のファイルを復号化されていない以下の修正コードを参照してください、との代わりにあなたのコードを追加します。 ToDo:

private void decryptButtonActionPerformed(java.awt.event.ActionEvent evt) {            
     try { 
      if(keyFile.getText() == null || keyFile.getText().equals("")) { 
       message.setText("Please select the key file"); 
      } 
      if(sourceDirectory.getText() == null || sourceDirectory.getText().equals("")) { 
       message.setText("Please select the source folder"); 
      } 
      if(destinationDirectory.getText() == null || destinationDirectory.getText().equals("")) { 
       message.setText("Please select the destination folder"); 
      } 

      Decoder decoder = new Decoder(); 

      File sourcedir = new File(sourceDirectory.getText()); 
      File destinationdir = new File(destinationDirectory.getText()); 

     // System.out.println(seeder); 

      String seeder = new String(decoder.getFileDataToStream(keyFile.getText())); 

      System.out.println(seeder); 
      File[] directoryListing = sourcedir.listFiles(); 
      if (directoryListing != null) { 
       for (File child : directoryListing) { 
        try{ 
         System.out.println(child.getCanonicalPath()); 
         byte[] inputstream = decoder.getFileDataToStream(child.getCanonicalPath()); 
         byte[] outputstream = decoder.decrypt(seeder, inputstream); 
         FileOutputStream fos = new FileOutputStream(destinationDirectory.getText()+File.separator+new String(Base64.decodeBase64(child.getName()))); 
         fos.write(outputstream); 
         fos.close(); 
        } 
        catch(Exception e){ 
         // ToDo: handle the case when file was not decrypted 
        } 
       } 
       message.setText("Decryption done"); 
      } else { 
       message.setText("Error while Listing the Source Directory Files"); 
      } 
     } catch (IOException ex) { 
      Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (Exception ex) { 
      Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 

問題2:フォルダー内のすべてのフォルダーを取得します。完全なフォルダ階層

LinkedList<String> folders = new LinkedList<String>(); 

folders.add("c:\\"); 

while(!folders.isEmpty()){ 

    File file = new File(folders.poll()); 

    if(file.listFiles()!=null) 
     for(File dir:file.listFiles()){ 

      if(dir.isDirectory()){ 
       folders.add(dir.getAbsolutePath()); 
       System.out.println("Folder " + dir.getAbsolutePath()); 
      } 
     } 

} 
+0

問題1つのテストショーメッセージ「復号化を行う」が、復号化後のファイル(暗号化解除)を持っていないフォルダの出力にエラーが発生したため – beli34

+0

ファイルには、catch節に対処する必要があると解読されることはありませんすべてのファイルが復号化されないのですか? –

関連する問題