java - 在 Java 中对输入/输出流进行加密/解密?

标签 java security encryption stream

很难说出这里问的是什么。这个问题是模棱两可的、含糊的、不完整的、过于宽泛或修辞的,不能以其目前的形式得到合理的回答。如需帮助澄清此问题以便可以重新打开,visit the help center .




8 年前关闭。




有没有办法加密和解密 Java 中的输入和输出流?
谢谢

最佳答案

查看 CipherInputStream 和 CipherOutputStream。

A CipherInputStream is composed of an InputStream and a Cipher so that read() methods return data that are read in from the underlying InputStream but have been additionally processed by the Cipher. The Cipher must be fully initialized before being used by a CipherInputStream.

For example, if the Cipher is initialized for decryption, the CipherInputStream will attempt to read in data and decrypt them, before returning the decrypted data.

This class adheres strictly to the semantics, especially the failure semantics, of its ancestor classes java.io.FilterInputStream and java.io.InputStream. This class has exactly those methods specified in its ancestor classes, and overrides them all. Moreover, this class catches all exceptions that are not thrown by its ancestor classes. In particular, the skip method skips, and the available method counts only data that have been processed by the encapsulated Cipher.

It is crucial for a programmer using this class not to use methods that are not defined or overriden in this class (such as a new method or constructor that is later added to one of the super classes), because the design and implementation of those methods are unlikely to have considered security impact with regard to CipherInputStream.



A CipherOutputStream is composed of an OutputStream and a Cipher so that write() methods first process the data before writing them out to the underlying OutputStream. The cipher must be fully initialized before being used by a CipherOutputStream.

For example, if the cipher is initialized for encryption, the CipherOutputStream will attempt to encrypt data before writing out the encrypted data.

This class adheres strictly to the semantics, especially the failure semantics, of its ancestor classes java.io.OutputStream and java.io.FilterOutputStream. This class has exactly those methods specified in its ancestor classes, and overrides them all. Moreover, this class catches all exceptions that are not thrown by its ancestor classes.

It is crucial for a programmer using this class not to use methods that are not defined or overriden in this class (such as a new method or constructor that is later added to one of the super classes), because the design and implementation of those methods are unlikely to have considered security impact with regard to CipherOutputStream.

关于java - 在 Java 中对输入/输出流进行加密/解密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13960927/

相关文章:

python - 在 Django 中验证上传的文件

java - 如何在 C# 中使用证书文件验证签名值?

java - corejava book (11ed) 6.4节中代码理解的一个问题

java - 尝试理解RSA加密代码示例

encryption - C# 中的 AES 加密和 CryptoJS 中的解密

java - IZpack java条件调试(IZpack 4)

java - CardLayout(swing)在按钮上有 Action 监听器?

java - utf-8 转换并不总是有效

java - 如何使用 Hibernate 避免 MySQL 数据库中的重复条目?

security - 使用 channel 加密 (https) 是否会使 key 散列变得多余?