简体   繁体   English

USB驱动器上的AES Java无法正常工作

[英]AES java on usb drive not working

I have Java class that decrypt a file based on a user password. 我有一个Java类,可根据用户密码解密文件。 I have a demo.jar and a file content.txt that contain the encrypted content. 我有一个demo.jar和一个包含加密内容的content.txt文件。 The program runs and based on the user password decrypt the file into a file called content_recovered.txt 该程序运行,并根据用户密码将该文件解密为一个名为content_recovered.txt的文件

As long as I do this on my computer, it works great. 只要我在计算机上执行此操作,它就可以很好地工作。 I put the files on a flash drive and run it from there and it does not work. 我将文件放在闪存驱动器上,然后从那里运行它,它不起作用。 The file content_recovered.txt is created but empty ! 文件content_recovered.txt已创建,但为空!

Anybody has an idea on how to make it work from a flash drive ? 有人对如何从闪存驱动器上起作用有想法吗?

The only reason that I can think why this would not work is that the buffer size on a USB might be larger than on disk and you might not be flushing your output stream buffers properly (or closing your input file properly). 我可以认为这样做不起作用的唯一原因是USB上的缓冲区大小可能大于磁盘上的大小,并且您可能没有正确刷新输出流缓冲区(或正确关闭了输入文件)。

Check your io code to make sure that you are flushing and closing when writing, and making sure that there are no other file references open when reading: 检查您的io代码,以确保在写入时正在刷新和关闭,并确保在读取时没有打开其他文件引用:

OutputSteam os = null;

try {
   os = new BufferedOutputStream(new FileOutputStream(folder,"content.txt"));
   writeEncryptedFile(os); // do your file writing here
} catch (Exception e) {
   e.printStackTrace(); // whatever your error logging is here.
} finally {
   if (os != null) {
      // MUST ALWAYS FLUSH BEFORE CLOSING OUTPUTSTREAM
      try { os.flush(); } catch (Exception e) {}
      try { os.close(); } catch (Exception e) {}
   }
}

Also - make sure that you close your input files in the finally block too. 另外-确保您也关闭了finally块中的输入文件。 This has caught me out on many occasions - not any more though :) 这在很多场合吸引了我-尽管不再有:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM