尝试解密文件时,java CipherInputStream为空
我正在尝试编写一些助手方法来处理读取和写入加密文件的问题。我有两种方法可以成功地实现这一点,并返回一个InputStream或一个OutputStream(实际上是密码版本),我可以使用它来读取或写入文件。我已经证实,当使用对象流包装并用于将加密对象读写到文件时,这些方法非常有效
然而,当我试图读取加密文本文件时,问题就出现了。我可以验证我输入的字符串是否被加密并写入正确的文件,但当我尝试从该文件读回时,BufferedReader会报告一个EOF(null)。输入流。available()方法返回0。我可以保证文件在那里,正在被找到,并且InputStream本身不是空的。有人能告诉我是什么原因吗
读/写加密对象的工作非常出色(这里的CorruptedStreamException很好):
private static void testWriteObject() {
String path = "derp.derp";
Derp start = new Derp("Asymmetril: " + message, 12543, 21.4, false);
FilesEnDe.writeEncryptedObject(key, "derp.derp", start);
echo("original");
echo(">"+start);
Object o;
try {
ObjectInputStream ois = new ObjectInputStream(ResourceManager.getResourceStatic(path));
o = ois.readObject();
echo("encrypted");
echo(">"+o);
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
o = FilesEnDe.readEncryptedObject(key, path);
echo("decrypted");
echo(">"+o);
}
输出:
original
>Asymmetril: WE CAME, WE SAW, WE CONQUERED.; 12543; 21.4; false
[RM] > Trying to load resource: derp.derp
java.io.StreamCorruptedException
[RM] > Trying to load resource: derp.derp
decrypted
>Asymmetril: WE CAME, WE SAW, WE CONQUERED.; 12543; 21.4; false
尝试解密文本文件不会(请注意,加密的文本是可读的):
private static void testWriteFile() {
String path = "EncryptedOut.txt";
BufferedReader bis1, bis2;
try {
BufferedOutputStream os = new BufferedOutputStream(FilesEnDe.getEncryptedOutputStream(key, path));
os.write(message.getBytes());
os.flush();
os.close();
} catch (IOException e1) {
e1.printStackTrace();
}
echo("original");
echo(">"+message);
try {
bis1 = new BufferedReader (new InputStreamReader(ResourceManager.getResourceStatic(path)));
echo("encrypted");
echo(">" + bis1.readLine());
bis1.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
InputStream is = FilesEnDe.getEncryptedInputStream(key, path);
InputStreamReader isr = new InputStreamReader(is);
bis2 = new BufferedReader (isr);
echo("bits in stream? " + is.available());
echo("decrypted");
echo(">"+bis2.readLine());
bis2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
输出:
original
>WE CAME, WE SAW, WE CONQUERED.
encrypted
>¤ƒ]£¬Vß4E?´?ùûe
[RM] > Trying to load resource: EncryptedOut.txt
bytes in stream? 0
decrypted
>null
用于创建CipherInputStream的代码:
public static InputStream getEncryptedInputStream(String key, String path) {
try {
InputStream is = ResourceManager.getResourceStatic(path);
SecretKeySpec keyspec = new SecretKeySpec(getHash(key),"AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, keyspec);
return new CipherInputStream(is,c);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return null;
}
当我尝试使用CIPHERINPUTSTREAM解密文件并检索原始字符串时,就会出现问题
# 1 楼答案
没有“加密文本文件”这样的东西。加密的结果是二进制的,而不是文本
你不应该使用
BufferedReader.
它不是文本,而是二进制的。使用BufferedInputStream.
# 2 楼答案
无论我是通过PrintWriter还是BufferedOutputStream写作,也不管我是否使用阅读器阅读。结果,我忘了关闭创建文件的OutputStream。我一加上那一行,一切都开始运转了。感谢Antoniosss建议我重做我的方法中损坏的部分。我想知道为什么Eclipse没有提到关于它的资源泄漏