Class CipherOutputStream

java.lang.Object
java.io.OutputStream
tech.oxfordsemantic.jrdfox.io.CipherOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class CipherOutputStream extends OutputStream
A class which wraps an output stream, encrypting the data written to it using AES-256-CBC. The output stream is provided 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 to be 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
    Creates a new CipherOutputStream object to encrypt the data before passing it into the provided output stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Flushes, then closes the output stream.
    void
    Flushes the output stream.
    void
    Initializes the cipher with the provided key and writes the initialization vector to the output stream.
    void
    write(byte[] data, int dataOffset, int numberOfBytesToWrite)
    Writes the specified byte array to the output stream.
    void
    write(int b)
    Writes the specified byte to the output stream.

    Methods inherited from class OutputStream

    nullOutputStream, write

    Methods inherited from class Object

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

    • CipherOutputStream

      public CipherOutputStream(OutputStream outputStream) throws IOException
      Creates a new CipherOutputStream object to encrypt the data before passing it into the provided output stream.
      Parameters:
      outputStream - the output stream to which the data will be written
      Throws:
      IOException - if an I/O error occurs
  • Method Details

    • initialize

      public void initialize(Key key) throws InvalidKeyException, IOException
      Initializes the cipher with the provided key and writes the initialization vector to the output stream.
      Parameters:
      key - the key to be used for encryption
      Throws:
      InvalidKeyException - if the key is invalid
      IOException - if an I/O error occurs
    • write

      public void write(int b) throws IOException
      Writes the specified byte to the output stream.
      Specified by:
      write in class OutputStream
      Parameters:
      b - the byte to be written
      Throws:
      IOException - if an I/O error occurs
    • write

      public void write(byte[] data, int dataOffset, int numberOfBytesToWrite) throws IOException
      Writes the specified byte array to the output stream.
      Overrides:
      write in class OutputStream
      Parameters:
      data - the byte array to be written
      dataOffset - the start offset in the data array
      numberOfBytesToWrite - the number of bytes to write
      Throws:
      IOException - if an I/O error occurs
    • flush

      public void flush() throws IOException
      Flushes the output stream.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException - if an I/O error occurs
    • close

      public void close() throws IOException
      Flushes, then closes the output stream.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException - if an I/O error occurs