Class CipherInputStream
java.lang.Object
java.io.InputStream
tech.oxfordsemantic.jrdfox.io.CipherInputStream
- All Implemented Interfaces:
Closeable, AutoCloseable
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
ConstructorsConstructorDescriptionCipherInputStream(InputStream inputStream) Creates a new CipherInputStream object to decrypt the content of the provided input stream. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the input stream.booleaninitialize(Key key) Initializes the cipher with the provided key and reads the initialization vector from the input stream.intread()Reads the next byte of data from the input stream.intread(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 InputStream
available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Constructor Details
-
CipherInputStream
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:
IOException- if an I/O error occurs
-
-
Method Details
-
initialize
public boolean initialize(Key key) throws InvalidKeyException, InvalidAlgorithmParameterException, 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:
trueif the initialization was successful,falseif it is unable to read the IV.- Throws:
InvalidKeyException- if the key is invalidInvalidAlgorithmParameterException- if the algorithm parameters are invalidIOException- if an I/O error occurs
-
read
Reads the next byte of data from the input stream.- Specified by:
readin classInputStream- Returns:
- the next byte of data, or -1 if the end of the stream is reached
- Throws:
IOException- if an I/O error occurs
-
read
Reads up to len bytes of data from the input stream into an array of bytes.- Overrides:
readin classInputStream- Parameters:
bytes- the buffer into which the data is readoff- the start offset in the buffer at which the data is writtenlen- 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:
IOException- if an I/O error occurs
-
close
Closes the input stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- if an I/O error occurs
-