如何强制CipherOutputStream完成加密,但是将基础流打开? [英] How can I force a CipherOutputStream to finish encrypting but leave the underlying stream open?

查看:230
本文介绍了如何强制CipherOutputStream完成加密,但是将基础流打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CipherOutputStream支持另一个OutputStream。在我写完所有需要加密的数据到CipherOutputStream之后,我需要附加一些未加密的数据。



CipherOutputStream的文档表示调用 flush )不会强制加密器中的最后一个块;因为我需要调用 close()。但是, close()也关闭了底层的OutputStream,我仍然需要写更多。



强制最后一个区块加密,不关闭流?我需要编写自己的NonClosingCipherOutputStream吗?

解决方案

如果您没有参考密码,您可以将 FilterOutputStream 传递给创建 CipherOutputStream 的方法。在 FilterOutputStream 中,覆盖关闭方法,使其不会实际关闭流。


I have a CipherOutputStream backed by another OutputStream. After I have finished writing all the data I need encrypted to the CipherOutputStream, I need to append some unencrypted data.

The documentation for CipherOutputStream says that calling flush() will not force the final block out of the encryptor; for that I need to call close(). But close() also closes the underlying OutputStream, which I still need to write more to.

How can I force the last block out of the encryptor without closing the stream? Do I need to write my own NonClosingCipherOutputStream?

解决方案

If you don't have a reference to the Cipher, you could pass a FilterOutputStream to the method that creates the CipherOutputStream. In the FilterOutputStream, override the close method so that it doesn't actually close the stream.

这篇关于如何强制CipherOutputStream完成加密,但是将基础流打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆