Class CipherInputStream

java.lang.Object
java.io.InputStream
tech.oxfordsemantic.jrdfox.io.CipherInputStream
All Implemented Interfaces:
java.io.Closeable, java.lang.AutoCloseable

public class CipherInputStream
extends java.io.InputStream
A class which wraps an input stream and decrypts the data read from it using AES-256-CBC. It is assumed that the input stream contains data in the following format: Initialization vector (16 bytes) followed by a sequence of encrypted blocks. Each encrypted block is preceded by 8 bytes which are interpreted as an unsigned integer in little-endian encoding. The integer denotes the size (in bytes) of the data within the block (before padding is applied). The data within the block is padded to the next multiple of the cipher block size and encrypted using AES-256-CBC.
  • Constructor Summary

    Constructors 
    Constructor Description
    CipherInputStream​(java.io.InputStream inputStream)
    Creates a new CipherInputStream object to decrypt the content of the provided input stream.
  • Method Summary

    Modifier and Type Method Description
    void close()
    Closes the input stream.
    boolean initialize​(java.security.Key key)
    Initializes the cipher with the provided key and reads the initialization vector from the input stream.
    int read()
    Reads the next byte of data from the input stream.
    int read​(byte[] bytes, int off, int len)
    Reads up to len bytes of data from the input stream into an array of bytes.

    Methods inherited from class java.io.InputStream

    available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CipherInputStream

      public CipherInputStream​(java.io.InputStream inputStream) throws java.io.IOException
      Creates a new CipherInputStream object to decrypt the content of the provided input stream.
      Parameters:
      inputStream - the input stream from which the data will be read
      Throws:
      java.io.IOException - if an I/O error occurs
  • Method Details

    • initialize

      public boolean initialize​(java.security.Key key) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, java.io.IOException
      Initializes the cipher with the provided key and reads the initialization vector from the input stream.
      Parameters:
      key - the key to be used for decryption
      Returns:
      true if the initialization was successful, false if it is unable to read the IV.
      Throws:
      java.security.InvalidKeyException - if the key is invalid
      java.security.InvalidAlgorithmParameterException - if the algorithm parameters are invalid
      java.io.IOException - if an I/O error occurs
    • read

      public int read() throws java.io.IOException
      Reads the next byte of data from the input stream.
      Specified by:
      read in class java.io.InputStream
      Returns:
      the next byte of data, or -1 if the end of the stream is reached
      Throws:
      java.io.IOException - if an I/O error occurs
    • read

      public int read​(byte[] bytes, int off, int len) throws java.io.IOException
      Reads up to len bytes of data from the input stream into an array of bytes.
      Overrides:
      read in class java.io.InputStream
      Parameters:
      bytes - the buffer into which the data is read
      off - the start offset in the buffer at which the data is written
      len - the maximum number of bytes to 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:
      java.io.IOException - if an I/O error occurs
    • close

      public void close() throws java.io.IOException
      Closes the input stream.
      Specified by:
      close in interface java.lang.AutoCloseable
      Specified by:
      close in interface java.io.Closeable
      Overrides:
      close in class java.io.InputStream
      Throws:
      java.io.IOException - if an I/O error occurs