gpt4 book ai didi

java - 在 AWT 应用程序中处理图像解密 key

转载 作者:行者123 更新时间:2023-12-01 11:53:07 24 4
gpt4 key购买 nike

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

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 构造函数中生成 key ,但在 btnEncryptActionPerformedbtnDecryptActionPerformed 中构造对象。由于您选择在构造函数中生成 key ,因此您的类中只需要有一个 FunctionClass 实例。

您可以将FunctionClass Encrypt= new FunctionClass();设置为该类的静态变量。

但是有一个问题。您可能需要在另一个 session 中解密某些内容,而不是加密文件时。根据上述建议,在关闭并重新打开应用程序后,您将无法解密该文件。您需要某种持久层来保存 key 。该层可能需要使用主 key 进行加密。

关于java - 在 AWT 应用程序中处理图像解密 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28633724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com