Class CtrCryptoOutputStream
java.lang.Object
java.io.OutputStream
org.apache.commons.crypto.stream.CryptoOutputStream
org.apache.commons.crypto.stream.CtrCryptoOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Channel, WritableByteChannel
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.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCtrCryptoOutputStream(OutputStream out, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) Constructs aCtrCryptoOutputStream.protectedCtrCryptoOutputStream(OutputStream outputStream, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) Constructs aCtrCryptoOutputStream.protectedCtrCryptoOutputStream(WritableByteChannel channel, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) Constructs aCtrCryptoOutputStream.protectedCtrCryptoOutputStream(WritableByteChannel channel, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) Constructs aCtrCryptoOutputStream.CtrCryptoOutputStream(Properties props, OutputStream out, byte[] key, byte[] iv) Constructs aCtrCryptoOutputStream.CtrCryptoOutputStream(Properties properties, OutputStream outputStream, byte[] key, byte[] iv, long streamOffset) Constructs aCtrCryptoOutputStream.CtrCryptoOutputStream(Properties props, WritableByteChannel out, byte[] key, byte[] iv) Constructs aCtrCryptoOutputStream.CtrCryptoOutputStream(Properties properties, WritableByteChannel channel, byte[] key, byte[] iv, long streamOffset) Constructs aCtrCryptoOutputStream.protectedCtrCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) Constructs aCtrCryptoOutputStream.protectedCtrCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) Constructs aCtrCryptoOutputStream. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidencrypt()Does the encryption, input isCryptoOutputStream.inBufferand output isCryptoOutputStream.outBuffer.protected voidDoes final encryption of the last data.protected longGet the underlying stream offsetprotected voidOverrides theCryptoOutputStream.initCipher().protected voidsetStreamOffset(long streamOffset) Set the underlying stream offsetMethods inherited from class CryptoOutputStream
checkStream, close, flush, freeBuffers, getBufferSize, getCipher, getInBuffer, getOutBuffer, isOpen, write, write, writeMethods inherited from class OutputStream
write
-
Constructor Details
-
CtrCryptoOutputStream
protected CtrCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) throws 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:
IOException- if an I/O error occurs.
-
CtrCryptoOutputStream
protected CtrCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws 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:
IOException- if an I/O error occurs.
-
CtrCryptoOutputStream
protected CtrCryptoOutputStream(OutputStream out, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) throws 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:
IOException- if an I/O error occurs.
-
CtrCryptoOutputStream
protected CtrCryptoOutputStream(OutputStream outputStream, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws 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:
IOException- if an I/O error occurs.
-
CtrCryptoOutputStream
public CtrCryptoOutputStream(Properties props, OutputStream out, byte[] key, byte[] iv) throws IOException Constructs aCtrCryptoOutputStream.- Parameters:
props- ThePropertiesclass represents a set of properties.out- the output stream.key- crypto key for the cipher.iv- Initialization vector for the cipher.- Throws:
IOException- if an I/O error occurs.
-
CtrCryptoOutputStream
public CtrCryptoOutputStream(Properties properties, OutputStream outputStream, byte[] key, byte[] iv, long streamOffset) throws IOException Constructs aCtrCryptoOutputStream.- Parameters:
properties- ThePropertiesclass 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:
IOException- if an I/O error occurs.
-
CtrCryptoOutputStream
public CtrCryptoOutputStream(Properties props, WritableByteChannel out, byte[] key, byte[] iv) throws IOException Constructs aCtrCryptoOutputStream.- Parameters:
props- ThePropertiesclass represents a set of properties.out- the WritableByteChannel instance.key- crypto key for the cipher.iv- Initialization vector for the cipher.- Throws:
IOException- if an I/O error occurs.
-
CtrCryptoOutputStream
public CtrCryptoOutputStream(Properties properties, WritableByteChannel channel, byte[] key, byte[] iv, long streamOffset) throws IOException Constructs aCtrCryptoOutputStream.- Parameters:
properties- ThePropertiesclass 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:
IOException- if an I/O error occurs.
-
CtrCryptoOutputStream
protected CtrCryptoOutputStream(WritableByteChannel channel, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) throws 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:
IOException- if an I/O error occurs.
-
CtrCryptoOutputStream
protected CtrCryptoOutputStream(WritableByteChannel channel, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws 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:
IOException- if an I/O error occurs.
-
-
Method Details
-
encrypt
Does the encryption, input isCryptoOutputStream.inBufferand output isCryptoOutputStream.outBuffer.- Overrides:
encryptin classCryptoOutputStream- Throws:
IOException- if an I/O error occurs.
-
encryptFinal
Does final encryption of the last data.- Overrides:
encryptFinalin classCryptoOutputStream- Throws:
IOException- if an I/O error occurs.
-
getStreamOffset
Get the underlying stream offset- Returns:
- the underlying stream offset
-
initCipher
Overrides theCryptoOutputStream.initCipher(). Initializes the cipher.- Overrides:
initCipherin classCryptoOutputStream
-
setStreamOffset
Set the underlying stream offset- Parameters:
streamOffset- the underlying stream offset
-