[英]Is the compression happening after the encryption?
在審查加密方案時,我遇到了以下代碼:
@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;
}
據我所知,流的順序是數據首先被加密,然后(無用地)被壓縮。 這是正確的還是我誤解了 OutputStreams 上的排序方式?
謝謝
是的,您誤解(或閱讀)了訂購的運作方式。
在new ZipOutputStream(cos)
處, CipherOutputStream
被ZOS
包裝,因此數據首先被壓縮,然后傳遞給包裝的 stream,它將加密數據,然后將其傳遞給下一個包裝的 stream,依此類推。
最外層stream先拿到go處的數據,如果開啟壓縮,則return zipOutputStream;
被稱為,zipstream 是最外層的 stream。它是FilterOutputStream的慣用用法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.