While reviewing an encryption scheme, I came accross the following code:
@Override
public OutputStream encrypt(OutputStream outputStream) throws Exception {
// generate the IV for encryption
final byte[] encryptionIV = KeyFileUtil.randomBytes(16);
outputStream.write(encryptionIV);
// now create the encryption cipher
final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, getKey(), new GCMParameterSpec(128, encryptionIV));
// The CipherOutputStream shouldn't close the underlying stream
outputStream = new FilterOutputStream(outputStream) {
@Override
public void close() throws IOException {
// Do nothing
}
};
final CipherOutputStream cos = new CipherOutputStream(outputStream, cipher);
if (useZip) {
final ZipOutputStream zipOutputStream = new ZipOutputStream(cos) {
@Override
public void finish() throws IOException {
super.finish();
def.end();
}
@Override
public void flush() {
// Do nothing.
}
@Override
public void close() throws IOException {
try {
super.flush();
} catch (final IOException exception) {
// Continue and try to close.
}
super.close();
}
};
zipOutputStream.putNextEntry(new ZipEntry("ResourceContents"));
return zipOutputStream;
}
return cos;
}
As far as I understand the ordering of the streams is such that data is being encrypted first then (uselessly) compressed. Is this correct or am I misunderstanding how ordering works on OutputStreams?
Thanks