My goal here is to decrypt an entire file into another file.
This output loop for some reason will not write to addmsgOut if the cypherBufStream (a BufferedInputStream reading an input file) is too small i.e. around 128 bytes. When bringing in larger files 38kb and up it works fine. I keep banging my head against the wall trying to figure it out and would love some help.
// Decrypt M and H using RSA encryption
OutputStream addmsgOut = new BufferedOutputStream(new FileOutputStream("message.add-msg"));
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] piece = new byte[128];
int e;
ByteArrayOutputStream out4 = new ByteArrayOutputStream();
while ((e = cypherBufStream.read(piece)) != -1) {
out4.write(piece, 0, e);
addmsgOut.write(cipher.doFinal(out4.toByteArray()));
out4.reset();
}
A
BufferedOutputStream
does what the name says. It buffers the data ... to avoid wasting CPU cycles, syscalls, etc on small write requests.But what this means is that data is only written out to the output (in this case) file in the following circumstances:
flush()
on the stream, orclose()
on the stream.The other thing to note is that you perform a
write(byte[], ...)
that is larger than the stream's buffer, anything in the buffer is flushed and then the entire write is performed bypassing the buffer. (Note that this is implementation specific behavior ...)However, looking at your code, it looks like you are writing to the
BufferedOutputStream
in (roughly) 128 byte chunks. So my guess is that even in the large file cases you are not writing out the entire file, with this code.