public class CtrCryptoInputStream extends CryptoInputStream
CtrCryptoInputStream decrypts data. AES CTR mode is required in order to ensure that the plain text and cipher text have a 1:1 mapping. CTR crypto stream has stream characteristic which is useful for implement features like random seek. The decryption is buffer based. The key points of the decryption are (1) calculating the counter and (2) padding through stream position:
counter = base + pos/(algorithm blocksize); padding = pos%(algorithm blocksize);
The underlying stream offset is maintained as state. It is not thread-safe.EOS, STREAM_BUFFER_SIZE_KEY
Modifier | Constructor and Description |
---|---|
protected |
CtrCryptoInputStream(Input input,
CryptoCipher cipher,
int bufferSize,
byte[] key,
byte[] iv)
Constructs a
CtrCryptoInputStream . |
protected |
CtrCryptoInputStream(Input input,
CryptoCipher cipher,
int bufferSize,
byte[] key,
byte[] iv,
long streamOffset)
Constructs a
CtrCryptoInputStream . |
protected |
CtrCryptoInputStream(InputStream inputStream,
CryptoCipher cipher,
int bufferSize,
byte[] key,
byte[] iv)
Constructs a
CtrCryptoInputStream . |
protected |
CtrCryptoInputStream(InputStream inputStream,
CryptoCipher cipher,
int bufferSize,
byte[] key,
byte[] iv,
long streamOffset)
Constructs a
CtrCryptoInputStream . |
|
CtrCryptoInputStream(Properties properties,
InputStream inputStream,
byte[] key,
byte[] iv)
Constructs a
CtrCryptoInputStream . |
|
CtrCryptoInputStream(Properties properties,
InputStream inputStream,
byte[] key,
byte[] iv,
long streamOffset)
Constructs a
CtrCryptoInputStream . |
|
CtrCryptoInputStream(Properties properties,
ReadableByteChannel channel,
byte[] key,
byte[] iv)
Constructs a
CtrCryptoInputStream . |
|
CtrCryptoInputStream(Properties properties,
ReadableByteChannel in,
byte[] key,
byte[] iv,
long streamOffset)
Constructs a
CtrCryptoInputStream . |
protected |
CtrCryptoInputStream(ReadableByteChannel channel,
CryptoCipher cipher,
int bufferSize,
byte[] key,
byte[] iv)
Constructs a
CtrCryptoInputStream . |
protected |
CtrCryptoInputStream(ReadableByteChannel channel,
CryptoCipher cipher,
int bufferSize,
byte[] key,
byte[] iv,
long streamOffset)
Constructs a
CtrCryptoInputStream . |
Modifier and Type | Method and Description |
---|---|
protected void |
decrypt()
Does the decryption using inBuffer as input and outBuffer as output.
|
protected void |
decrypt(ByteBuffer buf,
int offset,
int len)
Decrypts all data in buf: total n bytes from given start position.
|
protected void |
decryptBuffer(ByteBuffer out)
Does the decryption using out as output.
|
protected void |
decryptInPlace(ByteBuffer buf)
Does the decryption using inBuffer as input and buf as output.
|
protected int |
decryptMore()
Decrypts more data by reading the under layer stream.
|
protected long |
getCounter(long position)
Gets the counter for input stream position.
|
protected byte[] |
getInitIV()
Gets the initialization vector.
|
protected byte |
getPadding(long position)
Gets the padding for input stream position.
|
protected long |
getStreamOffset()
Gets the offset of the stream.
|
protected long |
getStreamPosition()
Gets the position of the stream.
|
protected void |
initCipher()
Overrides the
initCipher() . |
protected byte |
postDecryption(long position)
This method is executed immediately after decryption.
|
int |
read(ByteBuffer buf)
Overrides the
read(ByteBuffer) . |
protected void |
resetCipher(long position)
Calculates the counter and iv, resets the cipher.
|
protected void |
resetStreamOffset(long offset)
Resets the underlying stream offset; clear
CryptoInputStream.inBuffer and
CryptoInputStream.outBuffer . |
void |
seek(long position)
Seeks the stream to a specific position relative to start of the under
layer stream.
|
protected void |
setStreamOffset(long streamOffset)
Sets the offset of stream.
|
long |
skip(long n)
Overrides the
CryptoInputStream.skip(long) . |
available, checkStream, close, decryptFinal, freeBuffers, getBufferSize, getCipher, getInput, getKey, getParams, isOpen, markSupported, read, read
mark, read, reset
protected CtrCryptoInputStream(Input input, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) throws IOException
CtrCryptoInputStream
.input
- the input data.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.IOException
- if an I/O error occurs.protected CtrCryptoInputStream(Input input, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException
CtrCryptoInputStream
.input
- the input data.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the stream.IOException
- if an I/O error occurs.protected CtrCryptoInputStream(InputStream inputStream, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) throws IOException
CtrCryptoInputStream
.inputStream
- the input stream.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.IOException
- if an I/O error occurs.protected CtrCryptoInputStream(InputStream inputStream, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException
CtrCryptoInputStream
.inputStream
- the InputStream instance.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the stream.IOException
- if an I/O error occurs.public CtrCryptoInputStream(Properties properties, InputStream inputStream, byte[] key, byte[] iv) throws IOException
CtrCryptoInputStream
.properties
- The Properties
class represents a set of
properties.inputStream
- the input stream.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.IOException
- if an I/O error occurs.public CtrCryptoInputStream(Properties properties, InputStream inputStream, byte[] key, byte[] iv, long streamOffset) throws IOException
CtrCryptoInputStream
.properties
- The Properties
class represents a set of
properties.inputStream
- the InputStream instance.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the stream.IOException
- if an I/O error occurs.public CtrCryptoInputStream(Properties properties, ReadableByteChannel channel, byte[] key, byte[] iv) throws IOException
CtrCryptoInputStream
.properties
- The Properties
class represents a set of
properties.channel
- the ReadableByteChannel instance.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.IOException
- if an I/O error occurs.public CtrCryptoInputStream(Properties properties, ReadableByteChannel in, byte[] key, byte[] iv, long streamOffset) throws IOException
CtrCryptoInputStream
.properties
- The Properties
class represents a set of
properties.in
- the ReadableByteChannel instance.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the stream.IOException
- if an I/O error occurs.protected CtrCryptoInputStream(ReadableByteChannel channel, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) throws IOException
CtrCryptoInputStream
.channel
- the ReadableByteChannel instance.cipher
- the cipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.IOException
- if an I/O error occurs.protected CtrCryptoInputStream(ReadableByteChannel channel, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException
CtrCryptoInputStream
.channel
- the ReadableByteChannel instance.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the stream.IOException
- if an I/O error occurs.protected void decrypt() throws IOException
decrypt
in class CryptoInputStream
IOException
- if an I/O error occurs.protected void decrypt(ByteBuffer buf, int offset, int len) throws IOException
buf
- The buffer into which bytes are to be transferred.offset
- the start offset in the data.len
- the maximum number of decrypted data bytes to read.IOException
- if an I/O error occurs.protected void decryptBuffer(ByteBuffer out) throws IOException
out
- the output ByteBuffer.IOException
- if an I/O error occurs.protected void decryptInPlace(ByteBuffer buf) throws IOException
+
n where p is the position
before decryption, n is the number of bytes decrypted. The buf's
limit will not have changed.buf
- The buffer into which bytes are to be transferred.IOException
- if an I/O error occurs.protected int decryptMore() throws IOException
decryptMore
in class CryptoInputStream
IOException
- if an I/O error occurs.protected long getCounter(long position)
position
- the given position in the data.protected byte[] getInitIV()
protected byte getPadding(long position)
position
- the given position in the data.protected long getStreamOffset()
protected long getStreamPosition()
protected void initCipher()
initCipher()
. Initializes the
cipher.initCipher
in class CryptoInputStream
protected byte postDecryption(long position) throws IOException
position
- the given position in the data..IOException
- if an I/O error occurs.public int read(ByteBuffer buf) throws IOException
read(ByteBuffer)
. Reads a
sequence of bytes from this channel into the given buffer.read
in interface ReadableByteChannel
read
in class CryptoInputStream
buf
- The buffer into which bytes are to be transferred.-1
if the
channel has reached end-of-stream.IOException
- if an I/O error occurs.protected void resetCipher(long position) throws IOException
position
- the given position in the data.IOException
- if an I/O error occurs.protected void resetStreamOffset(long offset) throws IOException
CryptoInputStream.inBuffer
and
CryptoInputStream.outBuffer
. This Typically happens during skip(long)
.offset
- the offset of the stream.IOException
- if an I/O error occurs.public void seek(long position) throws IOException
position
- the given position in the data.IOException
- if an I/O error occurs.protected void setStreamOffset(long streamOffset)
streamOffset
- the stream offset.public long skip(long n) throws IOException
CryptoInputStream.skip(long)
. Skips over and
discards n
bytes of data from this input stream.skip
in class CryptoInputStream
n
- the number of bytes to be skipped.IOException
- if an I/O error occurs.Copyright © 2016–2022 The Apache Software Foundation. All rights reserved.