Package org.apache.commons.crypto.stream
Class CryptoInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.commons.crypto.stream.CryptoInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.nio.channels.Channel,java.nio.channels.ReadableByteChannel
- Direct Known Subclasses:
CtrCryptoInputStream
public class CryptoInputStream extends java.io.InputStream implements java.nio.channels.ReadableByteChannelCryptoInputStream reads input data and decrypts data in stream manner. It supports any mode of operations such as AES CBC/CTR/GCM mode in concept.It is not thread-safe.
-
-
Field Summary
Fields Modifier and Type Field Description private intbufferSizeThe buffer size.(package private) CryptoCiphercipherThe CryptoCipher instance.private booleanclosedFlag to mark whether the input stream is closed.static intEOSThe index value when the end of the stream has been reached-1.private booleanfinalDoneFlag to mark whether do final of the cipher to end the decrypting stream.(package private) java.nio.ByteBufferinBufferInput data buffer.(package private) InputinputThe input data.(package private) java.security.KeykeyCrypto key for the cipher.private static intMIN_BUFFER_SIZEprivate byte[]oneByteBuf(package private) java.nio.ByteBufferoutBufferThe decrypted data buffer.private java.security.spec.AlgorithmParameterSpecparamsthe algorithm parametersprivate static intSTREAM_BUFFER_SIZE_DEFAULTThe default value of the buffer size for stream.static java.lang.StringSTREAM_BUFFER_SIZE_KEYThe configuration key of the buffer size for stream.
-
Constructor Summary
Constructors Modifier Constructor Description protectedCryptoInputStream(java.io.InputStream inputStream, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)Constructs aCryptoInputStream.CryptoInputStream(java.lang.String transformation, java.util.Properties properties, java.io.InputStream inputStream, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)Constructs aCryptoInputStream.CryptoInputStream(java.lang.String transformation, java.util.Properties properties, java.nio.channels.ReadableByteChannel channel, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)Constructs aCryptoInputStream.protectedCryptoInputStream(java.nio.channels.ReadableByteChannel channel, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)Constructs aCryptoInputStream.protectedCryptoInputStream(Input input, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)Constructs aCryptoInputStream.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Overrides theInputStream.available().(package private) static intcheckBufferSize(CryptoCipher cipher, int bufferSize)Checks and floors buffer size.protected voidcheckStream()Checks whether the stream is closed.(package private) static voidcheckStreamCipher(CryptoCipher cipher)Checks whether the cipher is supported streaming.voidclose()Overrides theInputStream.close().protected voiddecrypt()Does the decryption using inBuffer as input and outBuffer as output.protected voiddecryptFinal()Does final of the cipher to end the decrypting stream.protected intdecryptMore()Decrypts more data by reading the under layer stream.protected voidfreeBuffers()Forcibly free the direct buffers.(package private) static voidfreeDirectBuffer(java.nio.ByteBuffer buffer)Forcibly free the direct buffer.protected intgetBufferSize()Gets the buffer size.(package private) static intgetBufferSize(java.util.Properties props)Reads crypto buffer size.protected CryptoCiphergetCipher()Gets the internal CryptoCipher.protected InputgetInput()Gets the input.protected java.security.KeygetKey()Gets the key.protected java.security.spec.AlgorithmParameterSpecgetParams()Gets the specification of cryptographic parameters.protected voidinitCipher()Initializes the cipher.booleanisOpen()Overrides theChannel.isOpen().booleanmarkSupported()Overrides theInputStream.markSupported().intread()Overrides theInputStream.read().intread(byte[] array, int off, int len)Overrides theInputStream.read(byte[], int, int).intread(java.nio.ByteBuffer dst)Overrides theReadableByteChannel.read(ByteBuffer).longskip(long n)Overrides theInputStream.skip(long).
-
-
-
Field Detail
-
STREAM_BUFFER_SIZE_KEY
public static final java.lang.String STREAM_BUFFER_SIZE_KEY
The configuration key of the buffer size for stream.- See Also:
- Constant Field Values
-
STREAM_BUFFER_SIZE_DEFAULT
private static final int STREAM_BUFFER_SIZE_DEFAULT
The default value of the buffer size for stream.- See Also:
- Constant Field Values
-
MIN_BUFFER_SIZE
private static final int MIN_BUFFER_SIZE
- See Also:
- Constant Field Values
-
EOS
public static final int EOS
The index value when the end of the stream has been reached-1.- Since:
- 1.1
- See Also:
- Constant Field Values
-
oneByteBuf
private final byte[] oneByteBuf
-
cipher
final CryptoCipher cipher
The CryptoCipher instance.
-
bufferSize
private final int bufferSize
The buffer size.
-
key
final java.security.Key key
Crypto key for the cipher.
-
params
private final java.security.spec.AlgorithmParameterSpec params
the algorithm parameters
-
closed
private boolean closed
Flag to mark whether the input stream is closed.
-
finalDone
private boolean finalDone
Flag to mark whether do final of the cipher to end the decrypting stream.
-
input
Input input
The input data.
-
inBuffer
java.nio.ByteBuffer inBuffer
Input data buffer. The data starts at inBuffer.position() and ends at to inBuffer.limit().
-
outBuffer
java.nio.ByteBuffer outBuffer
The decrypted data buffer. The data starts at outBuffer.position() and ends at outBuffer.limit().
-
-
Constructor Detail
-
CryptoInputStream
protected CryptoInputStream(Input input, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.io.IOException
Constructs aCryptoInputStream.- Parameters:
input- the input data.cipher- the cipher instance.bufferSize- the bufferSize.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
java.io.IOException- if an I/O error occurs.
-
CryptoInputStream
protected CryptoInputStream(java.io.InputStream inputStream, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.io.IOExceptionConstructs aCryptoInputStream.- Parameters:
cipher- the cipher instance.inputStream- the input stream.bufferSize- the bufferSize.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
java.io.IOException- if an I/O error occurs.
-
CryptoInputStream
protected CryptoInputStream(java.nio.channels.ReadableByteChannel channel, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.io.IOExceptionConstructs aCryptoInputStream.- Parameters:
channel- the ReadableByteChannel instance.cipher- the cipher instance.bufferSize- the bufferSize.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
java.io.IOException- if an I/O error occurs.
-
CryptoInputStream
public CryptoInputStream(java.lang.String transformation, java.util.Properties properties, java.io.InputStream inputStream, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.io.IOExceptionConstructs aCryptoInputStream.- Parameters:
transformation- the name of the transformation, e.g., AES/CBC/PKCS5Padding. See the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard transformation names.properties- ThePropertiesclass represents a set of properties.inputStream- the input stream.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
java.io.IOException- if an I/O error occurs.
-
CryptoInputStream
public CryptoInputStream(java.lang.String transformation, java.util.Properties properties, java.nio.channels.ReadableByteChannel channel, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.io.IOExceptionConstructs aCryptoInputStream.- Parameters:
transformation- the name of the transformation, e.g., AES/CBC/PKCS5Padding. See the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard transformation names.properties- ThePropertiesclass represents a set of properties.channel- the ReadableByteChannel object.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
java.io.IOException- if an I/O error occurs.
-
-
Method Detail
-
checkBufferSize
static int checkBufferSize(CryptoCipher cipher, int bufferSize)
Checks and floors buffer size.- Parameters:
cipher- theCryptoCipherinstance.bufferSize- the buffer size.- Returns:
- the remaining buffer size.
-
checkStreamCipher
static void checkStreamCipher(CryptoCipher cipher) throws java.io.IOException
Checks whether the cipher is supported streaming.- Parameters:
cipher- theCryptoCipherinstance.- Throws:
java.io.IOException- if an I/O error occurs.
-
freeDirectBuffer
static void freeDirectBuffer(java.nio.ByteBuffer buffer)
Forcibly free the direct buffer.- Parameters:
buffer- the bytebuffer to be freed.
-
getBufferSize
static int getBufferSize(java.util.Properties props)
Reads crypto buffer size.- Parameters:
props- ThePropertiesclass represents a set of properties.- Returns:
- the buffer size.
-
available
public int available() throws java.io.IOExceptionOverrides theInputStream.available(). Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.- Overrides:
availablein classjava.io.InputStream- Returns:
- an estimate of the number of bytes that can be read (or skipped
over) from this input stream without blocking or
0when it reaches the end of the input stream. - Throws:
java.io.IOException- if an I/O error occurs.
-
checkStream
protected void checkStream() throws java.io.IOExceptionChecks whether the stream is closed.- Throws:
java.io.IOException- if an I/O error occurs.
-
close
public void close() throws java.io.IOExceptionOverrides theInputStream.close(). Closes this input stream and releases any system resources associated with the stream.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException- if an I/O error occurs.
-
decrypt
protected void decrypt() throws java.io.IOExceptionDoes the decryption using inBuffer as input and outBuffer as output. Upon return, inBuffer is cleared; the decrypted data starts at outBuffer.position() and ends at outBuffer.limit().- Throws:
java.io.IOException- if an I/O error occurs.
-
decryptFinal
protected void decryptFinal() throws java.io.IOExceptionDoes final of the cipher to end the decrypting stream.- Throws:
java.io.IOException- if an I/O error occurs.
-
decryptMore
protected int decryptMore() throws java.io.IOExceptionDecrypts more data by reading the under layer stream. The decrypted data will be put in the output buffer. If the end of the under stream reached, we will do final of the cipher to finish all the decrypting of data.- Returns:
- The number of decrypted data. return -1 (if end of the decrypted stream) return 0 (no data now, but could have more later)
- Throws:
java.io.IOException- if an I/O error occurs.
-
freeBuffers
protected void freeBuffers()
Forcibly free the direct buffers.
-
getBufferSize
protected int getBufferSize()
Gets the buffer size.- Returns:
- the bufferSize.
-
getCipher
protected CryptoCipher getCipher()
Gets the internal CryptoCipher.- Returns:
- the cipher instance.
-
getInput
protected Input getInput()
Gets the input.- Returns:
- the input.
-
getKey
protected java.security.Key getKey()
Gets the key.- Returns:
- the key.
-
getParams
protected java.security.spec.AlgorithmParameterSpec getParams()
Gets the specification of cryptographic parameters.- Returns:
- the params.
-
initCipher
protected void initCipher() throws java.io.IOExceptionInitializes the cipher.- Throws:
java.io.IOException- if an I/O error occurs.
-
isOpen
public boolean isOpen()
Overrides theChannel.isOpen().- Specified by:
isOpenin interfacejava.nio.channels.Channel- Returns:
trueif, and only if, this channel is open.
-
markSupported
public boolean markSupported()
Overrides theInputStream.markSupported().- Overrides:
markSupportedin classjava.io.InputStream- Returns:
- false,the
CtrCryptoInputStreamdon't support the mark method.
-
read
public int read() throws java.io.IOExceptionOverrides theInputStream.read(). Reads the next byte of data from the input stream.- Specified by:
readin classjava.io.InputStream- Returns:
- the next byte of data, or
EOS (-1)if the end of the stream is reached. - Throws:
java.io.IOException- if an I/O error occurs.
-
read
public int read(byte[] array, int off, int len) throws java.io.IOExceptionOverrides theInputStream.read(byte[], int, int). Decryption is buffer based. If there is data inoutBuffer, then read it out of this buffer. If there is no data inoutBuffer, then read more from the underlying stream and do the decryption.- Overrides:
readin classjava.io.InputStream- Parameters:
array- the buffer into which the decrypted data is read.off- the buffer offset.len- the maximum number of decrypted data bytes to read.- Returns:
- int the total number of decrypted data bytes read into the buffer.
- Throws:
java.io.IOException- if an I/O error occurs.
-
read
public int read(java.nio.ByteBuffer dst) throws java.io.IOExceptionOverrides theReadableByteChannel.read(ByteBuffer). Reads a sequence of bytes from this channel into the given buffer.- Specified by:
readin interfacejava.nio.channels.ReadableByteChannel- Parameters:
dst- The buffer into which bytes are to be transferred.- Returns:
- The number of bytes read, possibly zero, or
EOS (-1)if the channel has reached end-of-stream. - Throws:
java.io.IOException- if an I/O error occurs.
-
skip
public long skip(long n) throws java.io.IOExceptionOverrides theInputStream.skip(long). Skips over and discardsnbytes of data from this input stream.- Overrides:
skipin classjava.io.InputStream- Parameters:
n- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
- Throws:
java.io.IOException- if an I/O error occurs.
-
-