CipherInputStream is composed of an InputStream
and a Cipher object so that read() methods return data that are
read in from the underlying InputStream but have been
additionally processed by the Cipher object. The Cipher
object must be fully initialized before being used by a
CipherInputStream.
For example, if the Cipher object 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 object.
This class may catch BadPaddingException and other exceptions
thrown by failed integrity checks during decryption. These exceptions are not
re-thrown, so the client may not be informed that integrity checks
failed. Because of this behavior, this class may not be suitable
for use with decryption in an authenticated mode of operation (e.g. GCM).
Applications that require authenticated encryption can use the
Cipher API directly as an alternative to using this class.
It is crucial for a programmer using this class not to use
methods that are not defined or overridden 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.
java.io.InputStream , java.io.FilterInputStream , javax.crypto.Cipher , javax.crypto.CipherOutputStream
| Modifier and Type | Field and Description |
|---|---|
| private final Cipher | |
| private boolean | |
| private boolean | |
| private final byte[] | |
| private final InputStream | |
| private byte[] | |
| private int | |
| private int |
| Access | Constructor and Description |
|---|---|
| public | CipherInputStream(InputStream
the to-be-processed input stream is, Cipher an initialized c)Cipher objectConstructs a |
| protected | CipherInputStream(InputStream
the to-be-processed input stream is)Constructs a |
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: the number of bytes that can be read from this input stream without blocking.Overrides java. Returns the number of bytes that can be read from this input stream without blocking. |
| public void | close()
Overrides java. Implements java. Closes this input stream and releases any system resources associated with the stream. |
| private void | ensureCapacity(int
the input length (in bytes) inLen)Ensure obuffer is big enough for the next update or doFinal
operation, given the input length |
| private int | getMoreData()
Private convenience function, read in data from the underlying input stream and process them with cipher. |
| public boolean | Returns: false, since this class does not support the
mark and reset methods.Overrides java. Tests if this input stream supports the |
| public int | Returns: the next byte of data, or-1 if the end of the
stream is reached.Overrides java. Reads the next byte of data from this input stream. |
| public int | Returns: the total number of bytes read into the buffer, or-1 is there is no more data because the end of
the stream has been reached.the buffer into which the data is read. b)Overrides java. Reads up to |
| public int | Returns: the total number of bytes read into the buffer, or-1 if there is no more data because the end of
the stream has been reached.the buffer into which the data is read. b, int the start offset in the destination array
off, int bufthe maximum number of bytes read. len)Overrides java. Reads up to |
| public long | Returns: the actual number of bytes skipped.the number of bytes to be skipped. n)Overrides java. Skips |
| cipher | back to summary |
|---|---|
| private final Cipher cipher | |
| closed | back to summary |
|---|---|
| private boolean closed | |
| done | back to summary |
|---|---|
| private boolean done | |
| ibuffer | back to summary |
|---|---|
| private final byte[] ibuffer | |
| input | back to summary |
|---|---|
| private final InputStream input | |
| obuffer | back to summary |
|---|---|
| private byte[] obuffer | |
| ofinish | back to summary |
|---|---|
| private int ofinish | |
| ostart | back to summary |
|---|---|
| private int ostart | |
| CipherInputStream | back to summary |
|---|---|
| public CipherInputStream(InputStream is, Cipher c) Constructs a Note if the specified input stream or cipher is
| |
| CipherInputStream | back to summary |
|---|---|
| protected CipherInputStream(InputStream is) Constructs a Note if the specified input stream is
| |
| available | back to summary |
|---|---|
| public int available() throws IOException Overrides java. Returns the number of bytes that can be read from this input
stream without blocking. The
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. Closes this input stream and releases any system resources associated with the stream.
The
| |
| ensureCapacity | back to summary |
|---|---|
| private void ensureCapacity(int inLen) Ensure obuffer is big enough for the next update or doFinal
operation, given the input length
| |
| getMoreData | back to summary |
|---|---|
| private int getMoreData() throws IOException Private convenience function, read in data from the underlying input stream and process them with cipher. This method is called when the processed bytes inside obuffer has been exhausted. Entry condition: ostart = ofinish Exit condition: ostart = 0 AND ostart <= ofinish return (ofinish-ostart) (we have this many bytes for you) return 0 (no data now, but could have more later) return -1 (absolutely no more data) Note: Exceptions are only thrown after the stream is completely read. For AEAD ciphers a read() of any length will internally cause the whole stream to be read fully and verify the authentication tag before returning decrypted data or exceptions. | |
| markSupported | back to summary |
|---|---|
| public boolean markSupported() Overrides java. Tests if this input stream supports the
| |
| read | back to summary |
|---|---|
| public int read() throws IOException Overrides java. Reads the next byte of data from this input stream. The value
byte is returned as an
| |
| read | back to summary |
|---|---|
| public int read(byte[] b) throws IOException Overrides java. Reads up to
The
| |
| read | back to summary |
|---|---|
| public int read(byte[] b, int off, int len) throws IOException Overrides java. Reads up to
| |
| skip | back to summary |
|---|---|
| public long skip(long n) throws IOException Overrides java. Skips Fewer bytes than requested might be skipped.
The actual number of bytes skipped is equal to The actual number of bytes skipped is returned.
| |