Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:未捕获故意的BadPaddingException_Java_Try Catch_Badpaddingexception - Fatal编程技术网

Java:未捕获故意的BadPaddingException

Java:未捕获故意的BadPaddingException,java,try-catch,badpaddingexception,Java,Try Catch,Badpaddingexception,我的应用程序提示用户输入用于加密控制文件的密码。如果输入了错误的密码,应用程序将通过创建新的控制文件进行响应。因此,我需要捕获BadPaddingException,以便触发适当的响应 下面是应该生成异常的代码段 private void existingHashFile(String file) { psUI = new passwordUI(new javax.swing.JFrame(), true, "existing"); psUI.setVisible

我的应用程序提示用户输入用于加密控制文件的密码。如果输入了错误的密码,应用程序将通过创建新的控制文件进行响应。因此,我需要捕获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!");
        }
为了完整起见,下面是上面调用的decryptHashFile方法

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(); 
            }
        }
    }
当我故意输入错误的密码时,我看到了这个堆栈跟踪,但是我在示例中使用的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)
您的第197行抛出IOException,而不是BadPaddingException,因此该异常被后续的catch IOException ex捕获


在此之后,您不会显式地抛出其他异常,因此在解密hashfile之后没有其他要捕获的异常。

谢谢,现在您已经解释了这一点,看起来很明显。由于我在decryptHashFile方法中处理异常,如果密码错误,我的调用方法就没有机会运行代码。所以我需要将IOException抛出,而不是捕获它,我猜?您可以重新抛出它,或者只是不在decryptHashFile中捕获它,或者返回布尔成功,而不是在existingHashFile中捕获异常。。。这是你的选择