Class CipherOutputStream

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

public class CipherOutputStream
extends java.io.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
    CipherOutputStream​(java.io.OutputStream outputStream)
    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 close()
    Flushes, then closes the output stream.
    void flush()
    Flushes the output stream.
    void initialize​(java.security.Key key)
    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 java.io.OutputStream

    nullOutputStream, write

    Methods inherited from class java.lang.Object

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

    • CipherOutputStream

      public CipherOutputStream​(java.io.OutputStream outputStream) throws java.io.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:
      java.io.IOException - if an I/O error occurs
  • Method Details

    • initialize

      public void initialize​(java.security.Key key) throws java.security.InvalidKeyException, java.io.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:
      java.security.InvalidKeyException - if the key is invalid
      java.io.IOException - if an I/O error occurs
    • write

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

      public void write​(byte[] data, int dataOffset, int numberOfBytesToWrite) throws java.io.IOException
      Writes the specified byte array to the output stream.
      Overrides:
      write in class java.io.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:
      java.io.IOException - if an I/O error occurs
    • flush

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

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