2017-12-02 3 views
0

私のアプリケーションは、コントロールファイルを暗号化するために使用されたパスワードをユーザーに要求します。間違ったパスワードが入力された場合、アプリケーションは新しい制御ファイルを作成して応答します。したがって、私はBadPaddingExceptionをキャッチして、適切な応答をトリガーする必要があります。ここで Java:意図的にBadPaddingExceptionをキャッチしない

は例外完全性については

private void existingHashFile(String file) { 
     psUI = new passwordUI(new javax.swing.JFrame(), true, "existing"); 
     psUI.setVisible(true); 
     this.key = passwordUI.key; 
     try { 
      hash.decryptHashFile(file, this.key); //this is line 240 
     } catch (BadPaddingException ex) { 
      Logger.getLogger(homePage.class.getName()).log(Level.SEVERE, null, ex); 
      //then the file was not decrypted 
      System.out.println("BPE 2!"); 
     } catch (Exception ex) { 
      Logger.getLogger(homePage.class.getName()).log(Level.SEVERE, null, ex); 
      System.out.println("BPE 3!"); 
     } 

を生成する必要がありますコードスニペットですが、ここで私が意図的に間違ったパスワードを入力すると

public void decryptHashFile(String filename, String key) throws BadPaddingException, UnsupportedEncodingException, Exception { 
     FileInputStream fis = null; 
     FileOutputStream fos = null; 
     CipherInputStream cis = null; 
     String outFile = filename.replace(".enc", ""); 
     byte[] byteKey = key.getBytes("UTF-8"); 

     Cipher cipher = getCipher(byteKey, "decrypt"); 

     try { 
      fis = new FileInputStream(filename); 
      fos = new FileOutputStream(outFile); 
      cis = new CipherInputStream(fis, cipher); 
      byte[] buffer = new byte[1024]; 
      int read = cis.read(buffer); 
      while (read != -1) { 
       fos.write(buffer, 0, read); 
       read = cis.read(buffer); //this is line 197 
      } 
     } catch (IOException ex) { 
      Logger.getLogger(hashListClass.class.getName()).log(Level.SEVERE, null, ex); 
     } finally { 
      if (fos != null) { 
       fos.close(); 
      } 
      if (cis != null) { 
       cis.close(); 
      } 
      if (fis != null) { 
       fis.close(); 
      } 
     } 
    } 

の上に呼ばれdecryptHashFile方法だ、私はこのスタックを参照してください私のコード(例ではprintlnを使用しました)は実行されません:

Dec 02, 2017 2:31:34 PM appwatch.hashListClass decryptHashFile 
SEVERE: null 
java.io.IOException: javax.crypto.BadPaddingException: Given final block not properly padded 
    at javax.crypto.CipherInputStream.getMoreData(CipherInputStream.java:121) 
    at javax.crypto.CipherInputStream.read(CipherInputStream.java:239) 
    at javax.crypto.CipherInputStream.read(CipherInputStream.java:215) 
    at appwatch.hashListClass.decryptHashFile(hashListClass.java:197) 
    at appwatch.homePage.existingHashFile(homePage.java:240) 

答えて

1

CipherInputStream.read(あなたの197行目)はではなくIOExceptionを投げるので、例外は次のcatch (IOException ex)で捕捉されます。

その後、他の例外を明示的にスローしていないので、decryptHashFileの後にキャッチすることはありません。

+0

ありがとうございます。 decryptHashFileメソッド内で例外を処理しているので、私の呼び出しメソッドはcode-if-bad-passwordを実行する機会がありません。だから私はそれをキャッチするよりもIOExceptionを投げる必要があると思いますか? –

+0

あなたはそれを再スローするか、 'decryptHashFile'でキャッチしないか、' existingHashFile'で例外をキャッチするのではなく 'boolean success'を返します。これはあなたの選択です –

関連する問題