Package org.apache.commons.crypto.stream
Class CtrCryptoOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.commons.crypto.stream.CryptoOutputStream
-
- org.apache.commons.crypto.stream.CtrCryptoOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
,java.nio.channels.Channel
,java.nio.channels.WritableByteChannel
public class CtrCryptoOutputStream extends CryptoOutputStream
CtrCryptoOutputStream encrypts data. It is not thread-safe. AES CTR mode is required in order to ensure that the plain text and cipher text have a 1:1 mapping. The encryption is buffer based. The key points of the encryption are (1) calculating counter and (2) padding through stream position.
counter = base + pos/(algorithm blocksize); padding = pos%(algorithm blocksize);
The underlying stream offset is maintained as state.
This class should only be used with blocking sinks. Using this class to wrap a non-blocking sink may lead to high CPU usage.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
cipherReset
Flag to mark whether the cipher has been resetprivate byte[]
initIV
The initial IV.private byte[]
iv
Initialization vector for the cipher.private byte
padding
Padding = pos%(algorithm blocksize); Padding is put intoCryptoOutputStream.inBuffer
before any other data goes in.private long
streamOffset
Underlying stream offset.
-
Constructor Summary
Constructors Modifier Constructor Description protected
CtrCryptoOutputStream(java.io.OutputStream out, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv)
Constructs aCtrCryptoOutputStream
.protected
CtrCryptoOutputStream(java.io.OutputStream outputStream, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset)
Constructs aCtrCryptoOutputStream
.protected
CtrCryptoOutputStream(java.nio.channels.WritableByteChannel channel, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv)
Constructs aCtrCryptoOutputStream
.protected
CtrCryptoOutputStream(java.nio.channels.WritableByteChannel channel, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset)
Constructs aCtrCryptoOutputStream
.CtrCryptoOutputStream(java.util.Properties props, java.io.OutputStream out, byte[] key, byte[] iv)
Constructs aCtrCryptoOutputStream
.CtrCryptoOutputStream(java.util.Properties properties, java.io.OutputStream outputStream, byte[] key, byte[] iv, long streamOffset)
Constructs aCtrCryptoOutputStream
.CtrCryptoOutputStream(java.util.Properties props, java.nio.channels.WritableByteChannel out, byte[] key, byte[] iv)
Constructs aCtrCryptoOutputStream
.CtrCryptoOutputStream(java.util.Properties properties, java.nio.channels.WritableByteChannel channel, byte[] key, byte[] iv, long streamOffset)
Constructs aCtrCryptoOutputStream
.protected
CtrCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv)
Constructs aCtrCryptoOutputStream
.protected
CtrCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset)
Constructs aCtrCryptoOutputStream
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
encrypt()
Does the encryption, input isCryptoOutputStream.inBuffer
and output isCryptoOutputStream.outBuffer
.private void
encryptBuffer(java.nio.ByteBuffer out)
Does the encryption if the ByteBuffer data.protected void
encryptFinal()
Does final encryption of the last data.protected long
getStreamOffset()
Get the underlying stream offsetprotected void
initCipher()
Overrides theCryptoOutputStream.initCipher()
.private void
resetCipher()
Resets theCryptoOutputStream.cipher
: calculate counter andpadding
.protected void
setStreamOffset(long streamOffset)
Set the underlying stream offset-
Methods inherited from class org.apache.commons.crypto.stream.CryptoOutputStream
checkStream, close, flush, freeBuffers, getBufferSize, getCipher, getInBuffer, getOutBuffer, isOpen, write, write, write
-
-
-
-
Field Detail
-
streamOffset
private long streamOffset
Underlying stream offset.
-
initIV
private final byte[] initIV
The initial IV.
-
iv
private final byte[] iv
Initialization vector for the cipher.
-
padding
private byte padding
Padding = pos%(algorithm blocksize); Padding is put intoCryptoOutputStream.inBuffer
before any other data goes in. The purpose of padding is to put input data at proper position.
-
cipherReset
private boolean cipherReset
Flag to mark whether the cipher has been reset
-
-
Constructor Detail
-
CtrCryptoOutputStream
protected CtrCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) throws java.io.IOException
Constructs aCtrCryptoOutputStream
.- Parameters:
output
- the Output instance.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.- Throws:
java.io.IOException
- if an I/O error occurs.
-
CtrCryptoOutputStream
protected CtrCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws java.io.IOException
Constructs aCtrCryptoOutputStream
.- Parameters:
output
- the output stream.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 data.- Throws:
java.io.IOException
- if an I/O error occurs.
-
CtrCryptoOutputStream
protected CtrCryptoOutputStream(java.io.OutputStream out, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) throws java.io.IOException
Constructs aCtrCryptoOutputStream
.- Parameters:
out
- the output stream.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.- Throws:
java.io.IOException
- if an I/O error occurs.
-
CtrCryptoOutputStream
protected CtrCryptoOutputStream(java.io.OutputStream outputStream, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws java.io.IOException
Constructs aCtrCryptoOutputStream
.- Parameters:
outputStream
- the output stream.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 data.- Throws:
java.io.IOException
- if an I/O error occurs.
-
CtrCryptoOutputStream
public CtrCryptoOutputStream(java.util.Properties props, java.io.OutputStream out, byte[] key, byte[] iv) throws java.io.IOException
Constructs aCtrCryptoOutputStream
.- Parameters:
props
- TheProperties
class represents a set of properties.out
- the output stream.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.- Throws:
java.io.IOException
- if an I/O error occurs.
-
CtrCryptoOutputStream
public CtrCryptoOutputStream(java.util.Properties properties, java.io.OutputStream outputStream, byte[] key, byte[] iv, long streamOffset) throws java.io.IOException
Constructs aCtrCryptoOutputStream
.- Parameters:
properties
- TheProperties
class represents a set of properties.outputStream
- the output stream.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the data.- Throws:
java.io.IOException
- if an I/O error occurs.
-
CtrCryptoOutputStream
public CtrCryptoOutputStream(java.util.Properties props, java.nio.channels.WritableByteChannel out, byte[] key, byte[] iv) throws java.io.IOException
Constructs aCtrCryptoOutputStream
.- Parameters:
props
- TheProperties
class represents a set of properties.out
- the WritableByteChannel instance.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.- Throws:
java.io.IOException
- if an I/O error occurs.
-
CtrCryptoOutputStream
public CtrCryptoOutputStream(java.util.Properties properties, java.nio.channels.WritableByteChannel channel, byte[] key, byte[] iv, long streamOffset) throws java.io.IOException
Constructs aCtrCryptoOutputStream
.- Parameters:
properties
- TheProperties
class represents a set of properties.channel
- the WritableByteChannel instance.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the data.- Throws:
java.io.IOException
- if an I/O error occurs.
-
CtrCryptoOutputStream
protected CtrCryptoOutputStream(java.nio.channels.WritableByteChannel channel, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) throws java.io.IOException
Constructs aCtrCryptoOutputStream
.- Parameters:
channel
- the WritableByteChannel instance.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.- Throws:
java.io.IOException
- if an I/O error occurs.
-
CtrCryptoOutputStream
protected CtrCryptoOutputStream(java.nio.channels.WritableByteChannel channel, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws java.io.IOException
Constructs aCtrCryptoOutputStream
.- Parameters:
channel
- the WritableByteChannel 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 data.- Throws:
java.io.IOException
- if an I/O error occurs.
-
-
Method Detail
-
encrypt
protected void encrypt() throws java.io.IOException
Does the encryption, input isCryptoOutputStream.inBuffer
and output isCryptoOutputStream.outBuffer
.- Overrides:
encrypt
in classCryptoOutputStream
- Throws:
java.io.IOException
- if an I/O error occurs.
-
encryptBuffer
private void encryptBuffer(java.nio.ByteBuffer out) throws java.io.IOException
Does the encryption if the ByteBuffer data.- Parameters:
out
- the output ByteBuffer.- Throws:
java.io.IOException
- if an I/O error occurs.
-
encryptFinal
protected void encryptFinal() throws java.io.IOException
Does final encryption of the last data.- Overrides:
encryptFinal
in classCryptoOutputStream
- Throws:
java.io.IOException
- if an I/O error occurs.
-
getStreamOffset
protected long getStreamOffset()
Get the underlying stream offset- Returns:
- the underlying stream offset
-
initCipher
protected void initCipher()
Overrides theCryptoOutputStream.initCipher()
. Initializes the cipher.- Overrides:
initCipher
in classCryptoOutputStream
-
resetCipher
private void resetCipher() throws java.io.IOException
Resets theCryptoOutputStream.cipher
: calculate counter andpadding
.- Throws:
java.io.IOException
- if an I/O error occurs.
-
setStreamOffset
protected void setStreamOffset(long streamOffset)
Set the underlying stream offset- Parameters:
streamOffset
- the underlying stream offset
-
-