![](/img/trans.png)
[英]Decrypt and read mp3 file directly from CipherInputStream without storing
[英]CipherInputStream is Empty when trying to decrypt file
我正在嘗試編寫一些輔助方法來處理讀取和寫入加密文件。 我有兩種方法可以成功實現這一點,並返回一個 InputStream 或一個 OutputStream(實際上是 Cipher 版本),我可以用它們來讀取或寫入文件。 我已經確認這些方法在用對象流包裝並用於讀取和寫入加密對象到文件時非常有效。
但是,當我嘗試從加密文本文件中讀取時出現問題。 我可以驗證我提供給它的字符串是否被加密並寫入正確的文件,但是當我嘗試從該文件讀回時,BufferedReader 報告 EOF(空)。 InputStream.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;
}
當我嘗試使用密碼輸入流解密文件並檢索原始字符串時會出現問題。
但是,當我嘗試從加密文本文件中讀取時出現問題。
沒有“加密文本文件”這樣的東西。 加密的結果是二進制的,而不是文本。
我可以驗證我提供給它的字符串是否被加密並寫入正確的文件,但是當我嘗試從該文件讀回時,BufferedReader 報告 EOF(空)。
您不應該使用BufferedReader.
它不是文本,它是二進制的。 使用BufferedInputStream.
我是通過 PrintWriter 還是 BufferedOutputStream 編寫的,也不管我是否使用 Reader 閱讀都無關緊要。 原來,我忘了關閉創建文件的 OutputStream。 一旦我添加了那一行,一切都開始了。 感謝 Antoniossss 建議我重做我方法中損壞的部分。 我想知道為什么 Eclipse 沒有提到關於它的資源泄漏......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.