Package tech.oxfordsemantic.jrdfox.io
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.
-
Constructor Details
-
CipherInputStream
public CipherInputStream(java.io.InputStream inputStream) throws java.io.IOExceptionCreates 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.IOExceptionInitializes 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 invalidjava.security.InvalidAlgorithmParameterException
- if the algorithm parameters are invalidjava.io.IOException
- if an I/O error occurs
-
read
public int read() throws java.io.IOExceptionReads the next byte of data from the input stream.- Specified by:
read
in classjava.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.IOExceptionReads up to len bytes of data from the input stream into an array of bytes.- Overrides:
read
in classjava.io.InputStream
- 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:
java.io.IOException
- if an I/O error occurs
-
close
public void close() throws java.io.IOExceptionCloses the input stream.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
- Throws:
java.io.IOException
- if an I/O error occurs
-