在 AWT 应用程序中处理图像解密的密钥

Handling key for image decryption in AWT application

我有以下用于解密加密图像的代码。我的问题是,如果我一次对图像进行加密和对图像进行解密,它就可以毫无问题地完成。但是如果我加密图像并等待一段时间并尝试解密图像,它会抛出这个异常。

javax.crypto.BadPaddingException: Given final block not properly padded

下面是我解密的代码

public void decrypt(String srcPath, String destPath) {  
        File encryptedFile = new File(srcPath);  
        File decryptedFile = new File(destPath);  
        InputStream inStream = null;  
        OutputStream outStream = null;  
        try {  
            inStream = new FileInputStream(encryptedFile);  
            outStream = new FileOutputStream(decryptedFile);  
            byte[] buffer = new byte[1024];  
            int len;  
            while ((len = inStream.read(buffer)) > 0) {  
                  outStream.write(cipher.update(buffer, 0, len));  
                  outStream.flush();  
                  }  
                  outStream.write(cipher.doFinal());  //I guess this line throws the error 
                  inStream.close();  
                  outStream.close();  
              } catch (IllegalBlockSizeException ex) {  
                   System.out.println(ex);  
             } catch (BadPaddingException ex) {  
               System.out.println(ex);  
             } catch (InvalidKeyException ex) {  
                    System.out.println(ex);  
             } catch (FileNotFoundException ex) {  
                 System.out.println(ex);  
                 } catch (IOException ex) {  
                  System.out.println(ex);  
        }  
}

加密密码

 public FunctionClass() {  
        try {  
        keyGenerator = KeyGenerator.getInstance("Blowfish");  
        secretKey = keyGenerator.generateKey();  
        cipher = Cipher.getInstance("Blowfish");  
    } catch (NoSuchPaddingException ex) {  
        System.out.println(ex);  
    } catch (NoSuchAlgorithmException ex) {  
        System.out.println(ex);  
    }  
}

 public void encrypt(String srcPath, String destPath) {  
    File rawFile = new File(srcPath);  
    File encryptedFile = new File(destPath);  
    InputStream inStream = null;  
    OutputStream outStream = null;  
    try {  
        /** 
         * Initialize the cipher for encryption 
         */  
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);  
        /** 
         * Initialize input and output streams 
         */  
        inStream = new FileInputStream(rawFile);  
        outStream = new FileOutputStream(encryptedFile);  
        byte[] buffer = new byte[1024];  
        int len;  
        while ((len = inStream.read(buffer)) > 0) {  
            outStream.write(cipher.update(buffer, 0, len));  
            outStream.flush();  
        }  
        outStream.write(cipher.doFinal());  
        inStream.close();  
        outStream.close();  
    } catch (IllegalBlockSizeException ex) {  
        System.out.println(ex);  
    } catch (BadPaddingException ex) {  
        System.out.println(ex);  
    } catch (InvalidKeyException ex) {  
        System.out.println(ex);  
    } catch (FileNotFoundException ex) {  
        System.out.println(ex);  
    } catch (IOException ex) {  
        System.out.println(ex);  
    }  
}

按钮代码

 private void btnEncryptActionPerformed(java.awt.event.ActionEvent evt) {                                           
    int o=jFileChooser1.showOpenDialog(this);
    if(o==JFileChooser.APPROVE_OPTION)
    {
        File f=jFileChooser1.getSelectedFile();
        String path=f.getAbsolutePath();

        FunctionClass Encrypt= new FunctionClass();
               String directoryPath = "C:/Users/Desktop/";
                   String encryptedFile = "encryptedFile.jpg";  
        Encrypt.encrypt(path, directoryPath+encryptedFile);
    }
}

  private void btnDecryptActionPerformed(java.awt.event.ActionEvent evt) {                                           
                          int o=jFileChooser1.showOpenDialog(this);
    if(o==JFileChooser.APPROVE_OPTION)
    {
        File f=jFileChooser1.getSelectedFile();
        String path=f.getAbsolutePath();

        FunctionClass Encrypt= new FunctionClass();
               String directoryPath = "C:/Users/Desktop/";
      String decryptedFile = "decryptedFile.jpg"; 
        Encrypt.decrypt(path, directoryPath+decryptedFile);
    }
}                                          

问题是您在 FunctionClass 构造函数中生成了密钥,但您在 btnEncryptActionPerformedbtnDecryptActionPerformed 中都构造了对象。因为您选择在构造函数中生成密钥,所以您的 class.

中只需要一个 FunctionClass 实例

您可以使 FunctionClass Encrypt= new FunctionClass(); 成为 class 的静态变量。

但是有一个问题。与加密文件时相比,您可能需要在另一个会话中解密某些内容。根据上述建议,您将无法在关闭并重新打开应用程序后解密文件。您需要某种持久层来保存密钥。这一层可能需要用主密钥加密。