Package tech.oxfordsemantic.jrdfox.io
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.
-
Constructor Details
-
CipherOutputStream
public CipherOutputStream(java.io.OutputStream outputStream) throws java.io.IOExceptionCreates 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.IOExceptionInitializes 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 invalidjava.io.IOException
- if an I/O error occurs
-
write
public void write(int b) throws java.io.IOExceptionWrites the specified byte to the output stream.- Specified by:
write
in classjava.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.IOExceptionWrites the specified byte array to the output stream.- Overrides:
write
in classjava.io.OutputStream
- Parameters:
data
- the byte array to be writtendataOffset
- the start offset in the data arraynumberOfBytesToWrite
- the number of bytes to write- Throws:
java.io.IOException
- if an I/O error occurs
-
flush
public void flush() throws java.io.IOExceptionFlushes the output stream.- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
- if an I/O error occurs
-
close
public void close() throws java.io.IOExceptionFlushes, then closes the output stream.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.OutputStream
- Throws:
java.io.IOException
- if an I/O error occurs
-