我目前正在开发一个加密程序,并且在解密时遇到问题。由此产生的文件是空白的,我一直在试图找到约一个小时的原因。我的解密代码如下...Java解密返回空白
有人可以告诉我为什么我的数据可能会出现空白吗?
file = x;
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
file = new File(file.getAbsolutePath().substring(0,
file.getAbsolutePath().length() - 4));
FileOutputStream fos = new FileOutputStream(file);
byte k[] = Hash.MD5(password).getBytes("UTF-8");
SecretKeySpec key = new SecretKeySpec(k, "AES");
Cipher cipher = Cipher.getInstance(algorithm);
byte[] iv = batchIV;
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
CipherInputStream cin = new CipherInputStream(fis,
cipher);
byte[] buffer = new byte[1024];
int read = 0;
while ((read = cin.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.flush();
fos.close();
cin.close();
链接转到较大的代码部分。
解密方法在这里:http://pastebin.com/2p2juUTa
全部类别这里:http://pastebin.com/hgZHT4wg
我发现,CipherInputStream将返回-1,当您试图从中读取...我来仍不确定如果有人能够帮助,可能会导致这种情况。
检查我的答案在这里http://stackoverflow.com/questions/22619602/files-encryption-java/22620403#22620403 – Libin
我很抱歉,但我没有看到你的答案如何应用于此。我没有使用任何密码流。我有一个非常好的加密方法,但是,解密时出现问题,并且我无法找到为什么CipherInputStream在读取时返回-1。 – Kristoff
“我的加密代码在下面。”不,您的*解密*代码如下。你的加密代码在哪里?你确定你没有生成零长度的文件吗?或具有加密标题但没有数据的文件? – EJP