Class CipherInputStream

java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.bouncycastle.jcajce.io.CipherInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class CipherInputStream extends FilterInputStream
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 is a reimplementation of CipherInputStream that is safe for use with AEAD block ciphers, and does not silently catch BadPaddingException and IllegalBlockSizeException errors. Any errors that occur during finalisation are rethrown wrapped in an InvalidCipherTextIOException.

  • Constructor Details

    • CipherInputStream

      public CipherInputStream(InputStream input, Cipher cipher)
      Constructs a CipherInputStream from an InputStream and an initialised Cipher.
  • Method Details

    • read

      public int read() throws IOException
      Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and returns the next available byte.

      If the underlying stream is exhausted by this call, the cipher will be finalised.

      Overrides:
      read in class FilterInputStream
      Throws:
      IOException - if there was an error closing the input stream.
      InvalidCipherTextIOException - if the data read from the stream was invalid ciphertext (e.g. the cipher is an AEAD cipher and the ciphertext tag check fails).
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and then returns up to len bytes in the provided array.

      If the underlying stream is exhausted by this call, the cipher will be finalised.

      Overrides:
      read in class FilterInputStream
      Parameters:
      b - the buffer into which the data is read.
      off - the start offset in the destination array b
      len - the maximum number of bytes read.
      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.
      Throws:
      IOException - if there was an error closing the input stream.
      InvalidCipherTextIOException - if the data read from the stream was invalid ciphertext (e.g. the cipher is an AEAD cipher and the ciphertext tag check fails).
    • skip

      public long skip(long n) throws IOException
      Overrides:
      skip in class FilterInputStream
      Throws:
      IOException
    • available

      public int available() throws IOException
      Overrides:
      available in class FilterInputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Closes the underlying input stream, and then finalises the processing of the data by the cipher.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterInputStream
      Throws:
      IOException - if there was an error closing the input stream.
      InvalidCipherTextIOException - if the data read from the stream was invalid ciphertext (e.g. the cipher is an AEAD cipher and the ciphertext tag check fails).
    • mark

      public void mark(int readlimit)
      Overrides:
      mark in class FilterInputStream
    • reset

      public void reset() throws IOException
      Overrides:
      reset in class FilterInputStream
      Throws:
      IOException
    • markSupported

      public boolean markSupported()
      Overrides:
      markSupported in class FilterInputStream