压缩是在加密之后进行的吗?

huangapple go评论99阅读模式
英文:

Is the compression happening after the encryption?

问题

在审查加密方案时,我遇到了以下代码:

	@Override
	public OutputStream encrypt(OutputStream outputStream) throws Exception {

		// 生成用于加密的IV
		final byte[] encryptionIV = KeyFileUtil.randomBytes(16);
		outputStream.write(encryptionIV);

		// 现在创建加密密码器
		final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
		cipher.init(Cipher.ENCRYPT_MODE, getKey(), new GCMParameterSpec(128, encryptionIV));

		// CipherOutputStream不应关闭底层流
		outputStream = new FilterOutputStream(outputStream) {
			@Override
			public void close() throws IOException {
				// 什么都不做
			}
		};
		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() {
					// 什么都不做。
				}

				@Override
				public void close() throws IOException {
					try {
						super.flush();
					} catch (final IOException exception) {
						// 继续尝试关闭。
					}
					super.close();
				}
			};
			zipOutputStream.putNextEntry(new ZipEntry("ResourceContents"));
			return zipOutputStream;
		}
		return cos;
	}

据我理解,流的顺序是首先加密数据,然后(无效地)压缩数据。这是正确的吗,还是我对OutputStream的顺序理解有误?

谢谢。

英文:

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

答案1

得分: 5

是的,你误解了(或者阅读了)订单的工作方式。

new ZipOutputStream(cos)时,CipherOutputStreamZOS包装,因此数据首先被压缩,然后传递给包装的流,该流将对数据进行加密,然后将其传递给下一个包装的流,依此类推。

最外层的流首先处理数据,如果启用了压缩,将调用return zipOutputStream;,其中zipstream是最外层的流。这是对FilterOutputStream的成熟用法。

英文:

Yes, you're misunderstanding (or reading) how ordering works.

At new ZipOutputStream(cos) the CipherOutputStream is wrapped by the ZOS, therefore data is first zipped, then passed on to the wrapped stream, which will encrypt the data, and then pass it on to the next wrapped stream, and so on.

The outermost stream gets first go at the data, and if compression is enabled, return zipOutputStream; is called, with the zipstream being the outermost stream. It's idiomatic use of FilterOutputStream.

huangapple
  • 本文由 发表于 2020年7月29日 17:17:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63150327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定