@ThreadSafety(level=NOT_THREADSAFE) public final class PassphraseEncryptedOutputStream extends java.io.OutputStream
OutputStream
implementation that will encrypt
all data written to it with a key generated from a passphrase. Details about
the encryption will be encapsulated in a
PassphraseEncryptedStreamHeader
, which will typically be written to
the underlying stream before any of the encrypted data, so that the
PassphraseEncryptedInputStream
can read it to determine how to
decrypt that data when provided with the same passphrase. However, it is
also possible to store the encryption header elsewhere and provide it to the
PassphraseEncryptedInputStream
constructor so that that the
underlying stream will only include encrypted data.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_AES_128_CIPHER_TYPE_ITERATION_COUNT
The default PBKDF2 iteration count that should be used for the
PassphraseEncryptionCipherType.AES_128 cipher type. |
static int |
DEFAULT_AES_256_CIPHER_TYPE_ITERATION_COUNT
The default PBKDF2 iteration count that should be used for the
PassphraseEncryptionCipherType.AES_256 cipher type. |
static java.lang.String |
PROPERTY_DEFAULT_AES_128_CIPHER_TYPE_ITERATION_COUNT
The name of a system property that can be used to override the default
PBKDF2 iteration count for the
PassphraseEncryptionCipherType.AES_128 cipher type. |
static java.lang.String |
PROPERTY_DEFAULT_AES_256_CIPHER_TYPE_ITERATION_COUNT
The name of a system property that can be used to override the default
PBKDF2 iteration count for the
PassphraseEncryptionCipherType.AES_256 cipher type. |
Constructor and Description |
---|
PassphraseEncryptedOutputStream(char[] passphrase,
java.io.OutputStream wrappedOutputStream)
Creates a new passphrase-encrypted output stream with the provided
information.
|
PassphraseEncryptedOutputStream(char[] passphrase,
java.io.OutputStream wrappedOutputStream,
PassphraseEncryptedOutputStreamProperties properties)
Creates a new passphrase-encrypted output stream with the provided
information.
|
PassphraseEncryptedOutputStream(char[] passphrase,
java.io.OutputStream wrappedOutputStream,
java.lang.String keyIdentifier,
boolean useStrongEncryption,
boolean writeHeaderToStream)
Creates a new passphrase-encrypted output stream with the provided
information.
|
PassphraseEncryptedOutputStream(char[] passphrase,
java.io.OutputStream wrappedOutputStream,
java.lang.String keyIdentifier,
boolean useStrongEncryption,
int keyFactoryIterationCount,
boolean writeHeaderToStream)
Creates a new passphrase-encrypted output stream with the provided
information.
|
PassphraseEncryptedOutputStream(PassphraseEncryptedStreamHeader header,
java.io.OutputStream wrappedOutputStream,
boolean writeHeaderToStream)
Creates a new passphrase-encrypted output stream that wraps the provided
output stream and reuses the same derived secret key as the given
stream header (although with a newly computed initialization vector).
|
PassphraseEncryptedOutputStream(java.lang.String passphrase,
java.io.OutputStream wrappedOutputStream)
Creates a new passphrase-encrypted output stream with the provided
information.
|
PassphraseEncryptedOutputStream(java.lang.String passphrase,
java.io.OutputStream wrappedOutputStream,
PassphraseEncryptedOutputStreamProperties properties)
Creates a new passphrase-encrypted output stream with the provided
information.
|
PassphraseEncryptedOutputStream(java.lang.String passphrase,
java.io.OutputStream wrappedOutputStream,
java.lang.String keyIdentifier,
boolean useStrongEncryption,
boolean writeHeaderToStream)
Creates a new passphrase-encrypted output stream with the provided
information.
|
PassphraseEncryptedOutputStream(java.lang.String passphrase,
java.io.OutputStream wrappedOutputStream,
java.lang.String keyIdentifier,
boolean useStrongEncryption,
int keyFactoryIterationCount,
boolean writeHeaderToStream)
Creates a new passphrase-encrypted output stream with the provided
information.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this output stream, along with the underlying output stream.
|
void |
flush()
Flushes the underlying output stream so that any buffered encrypted output
will be written to the underlying output stream, and also flushes the
underlying output stream.
|
PassphraseEncryptedStreamHeader |
getEncryptionHeader()
Retrieves an encryption header with details about the encryption being
used.
|
void |
write(byte[] b)
Writes an encrypted representation of the contents of the provided byte
array to the underlying output stream.
|
void |
write(byte[] b,
int offset,
int length)
Writes an encrypted representation of the specified portion of the provided
byte array to the underlying output stream.
|
void |
write(int b)
Writes an encrypted representation of the provided byte to the underlying
output stream.
|
public static final int DEFAULT_AES_128_CIPHER_TYPE_ITERATION_COUNT
PassphraseEncryptionCipherType.AES_128
cipher type.public static final int DEFAULT_AES_256_CIPHER_TYPE_ITERATION_COUNT
PassphraseEncryptionCipherType.AES_256
cipher type.@NotNull public static final java.lang.String PROPERTY_DEFAULT_AES_128_CIPHER_TYPE_ITERATION_COUNT
PassphraseEncryptionCipherType.AES_128
cipher type.@NotNull public static final java.lang.String PROPERTY_DEFAULT_AES_256_CIPHER_TYPE_ITERATION_COUNT
PassphraseEncryptionCipherType.AES_256
cipher type.public PassphraseEncryptedOutputStream(@NotNull java.lang.String passphrase, @NotNull java.io.OutputStream wrappedOutputStream) throws java.security.GeneralSecurityException, java.io.IOException
PassphraseEncryptedStreamHeader
to the
underlying stream before writing any encrypted data.passphrase
- The passphrase that will be used to generate the encryption
key. It must not be null
.wrappedOutputStream
- The output stream to which the encrypted data (optionally
preceded by a header with details about the encryption) will
be written. It must not be null
.java.security.GeneralSecurityException
- If a problem is encountered while
initializing the encryption.java.io.IOException
- If a problem is encountered while writing the
encryption header to the underlying output stream.public PassphraseEncryptedOutputStream(@NotNull char[] passphrase, @NotNull java.io.OutputStream wrappedOutputStream) throws java.security.GeneralSecurityException, java.io.IOException
PassphraseEncryptedStreamHeader
to the
underlying stream before writing any encrypted data.passphrase
- The passphrase that will be used to generate the encryption
key. It must not be null
.wrappedOutputStream
- The output stream to which the encrypted data (optionally
preceded by a header with details about the encryption) will
be written. It must not be null
.java.security.GeneralSecurityException
- If a problem is encountered while
initializing the encryption.java.io.IOException
- If a problem is encountered while writing the
encryption header to the underlying output stream.public PassphraseEncryptedOutputStream(@NotNull java.lang.String passphrase, @NotNull java.io.OutputStream wrappedOutputStream, @Nullable java.lang.String keyIdentifier, boolean useStrongEncryption, boolean writeHeaderToStream) throws java.security.GeneralSecurityException, java.io.IOException
passphrase
- The passphrase that will be used to generate the encryption
key. It must not be null
.wrappedOutputStream
- The output stream to which the encrypted data (optionally
preceded by a header with details about the encryption) will
be written. It must not be null
.keyIdentifier
- An optional identifier that may be used to associate the
encryption details with information in another system. This
is primarily intended for use in conjunction with
UnboundID/Ping Identity products, but may be useful in other
systems. It may be null
if no key identifier is
needed.useStrongEncryption
- Indicates whether to attempt to use strong encryption, if it
is available. If this is true
and the JVM supports
the stronger level of encryption, then that encryption will be
used. If this is false
, or if the JVM does not
support the attempted stronger level of encryption, then the
baseline configuration will be used.writeHeaderToStream
- Indicates whether to write the generated
PassphraseEncryptedStreamHeader
to the provided
wrappedOutputStream
before any encrypted data so that
a PassphraseEncryptedInputStream
can read it to obtain
information necessary for decrypting the data. If this is
false
, then the getEncryptionHeader()
method
must be used to obtain the encryption header so that it can be
stored elsewhere and provided to the
PassphraseEncryptedInputStream
constructor.java.security.GeneralSecurityException
- If a problem is encountered while
initializing the encryption.java.io.IOException
- If a problem is encountered while writing the
encryption header to the underlying output stream.public PassphraseEncryptedOutputStream(@NotNull char[] passphrase, @NotNull java.io.OutputStream wrappedOutputStream, @Nullable java.lang.String keyIdentifier, boolean useStrongEncryption, boolean writeHeaderToStream) throws java.security.GeneralSecurityException, java.io.IOException
passphrase
- The passphrase that will be used to generate the encryption
key. It must not be null
.wrappedOutputStream
- The output stream to which the encrypted data (optionally
preceded by a header with details about the encryption) will
be written. It must not be null
.keyIdentifier
- An optional identifier that may be used to associate the
encryption details with information in another system. This
is primarily intended for use in conjunction with
UnboundID/Ping Identity products, but may be useful in other
systems. It may be null
if no key identifier is
needed.useStrongEncryption
- Indicates whether to attempt to use strong encryption, if it
is available. If this is true
and the JVM supports
the stronger level of encryption, then that encryption will be
used. If this is false
, or if the JVM does not
support the attempted stronger level of encryption, then the
baseline configuration will be used.writeHeaderToStream
- Indicates whether to write the generated
PassphraseEncryptedStreamHeader
to the provided
wrappedOutputStream
before any encrypted data so that
a PassphraseEncryptedInputStream
can read it to obtain
information necessary for decrypting the data. If this is
false
, then the getEncryptionHeader()
method
must be used to obtain the encryption header so that it can be
stored elsewhere and provided to the
PassphraseEncryptedInputStream
constructor.java.security.GeneralSecurityException
- If a problem is encountered while
initializing the encryption.java.io.IOException
- If a problem is encountered while writing the
encryption header to the underlying output stream.public PassphraseEncryptedOutputStream(@NotNull java.lang.String passphrase, @NotNull java.io.OutputStream wrappedOutputStream, @Nullable java.lang.String keyIdentifier, boolean useStrongEncryption, int keyFactoryIterationCount, boolean writeHeaderToStream) throws java.security.GeneralSecurityException, java.io.IOException
passphrase
- The passphrase that will be used to generate the encryption
key. It must not be null
.wrappedOutputStream
- The output stream to which the encrypted data (optionally
preceded by a header with details about the encryption) will
be written. It must not be null
.keyIdentifier
- An optional identifier that may be used to associate the
encryption details with information in another system. This
is primarily intended for use in conjunction with
UnboundID/Ping Identity products, but may be useful in other
systems. It may be null
if no key identifier is
needed.useStrongEncryption
- Indicates whether to attempt to use strong encryption, if it
is available. If this is true
and the JVM supports
the stronger level of encryption, then that encryption will be
used. If this is false
, or if the JVM does not
support the attempted stronger level of encryption, then the
baseline configuration will be used.keyFactoryIterationCount
- The iteration count to use when generating the encryption key
from the provided passphrase.writeHeaderToStream
- Indicates whether to write the generated
PassphraseEncryptedStreamHeader
to the provided
wrappedOutputStream
before any encrypted data so that
a PassphraseEncryptedInputStream
can read it to obtain
information necessary for decrypting the data. If this is
false
, then the getEncryptionHeader()
method
must be used to obtain the encryption header so that it can be
stored elsewhere and provided to the
PassphraseEncryptedInputStream
constructor.java.security.GeneralSecurityException
- If a problem is encountered while
initializing the encryption.java.io.IOException
- If a problem is encountered while writing the
encryption header to the underlying output stream.public PassphraseEncryptedOutputStream(@NotNull char[] passphrase, @NotNull java.io.OutputStream wrappedOutputStream, @Nullable java.lang.String keyIdentifier, boolean useStrongEncryption, int keyFactoryIterationCount, boolean writeHeaderToStream) throws java.security.GeneralSecurityException, java.io.IOException
passphrase
- The passphrase that will be used to generate the encryption
key. It must not be null
.wrappedOutputStream
- The output stream to which the encrypted data (optionally
preceded by a header with details about the encryption) will
be written. It must not be null
.keyIdentifier
- An optional identifier that may be used to associate the
encryption details with information in another system. This
is primarily intended for use in conjunction with
UnboundID/Ping Identity products, but may be useful in other
systems. It may be null
if no key identifier is
needed.useStrongEncryption
- Indicates whether to attempt to use strong encryption, if it
is available. If this is true
and the JVM supports
the stronger level of encryption, then that encryption will be
used. If this is false
, or if the JVM does not
support the attempted stronger level of encryption, then the
baseline configuration will be used.keyFactoryIterationCount
- The iteration count to use when generating the encryption key
from the provided passphrase.writeHeaderToStream
- Indicates whether to write the generated
PassphraseEncryptedStreamHeader
to the provided
wrappedOutputStream
before any encrypted data so that
a PassphraseEncryptedInputStream
can read it to obtain
information necessary for decrypting the data. If this is
false
, then the getEncryptionHeader()
method
must be used to obtain the encryption header so that it can be
stored elsewhere and provided to the
PassphraseEncryptedInputStream
constructor.java.security.GeneralSecurityException
- If a problem is encountered while
initializing the encryption.java.io.IOException
- If a problem is encountered while writing the
encryption header to the underlying output stream.public PassphraseEncryptedOutputStream(@NotNull java.lang.String passphrase, @NotNull java.io.OutputStream wrappedOutputStream, @NotNull PassphraseEncryptedOutputStreamProperties properties) throws java.security.GeneralSecurityException, java.io.IOException
passphrase
- The passphrase that will be used to generate the encryption
key. It must not be null
.wrappedOutputStream
- The output stream to which the encrypted data (optionally
preceded by a header with details about the encryption) will
be written. It must not be null
.properties
- The properties to use when encrypting data. It must not be
null
.java.security.GeneralSecurityException
- If a problem is encountered while
initializing the encryption.java.io.IOException
- If a problem is encountered while writing the
encryption header to the underlying output stream.public PassphraseEncryptedOutputStream(@NotNull char[] passphrase, @NotNull java.io.OutputStream wrappedOutputStream, @NotNull PassphraseEncryptedOutputStreamProperties properties) throws java.security.GeneralSecurityException, java.io.IOException
passphrase
- The passphrase that will be used to generate the encryption
key. It must not be null
.wrappedOutputStream
- The output stream to which the encrypted data (optionally
preceded by a header with details about the encryption) will
be written. It must not be null
.properties
- The properties to use when encrypting data. It must not be
null
.java.security.GeneralSecurityException
- If a problem is encountered while
initializing the encryption.java.io.IOException
- If a problem is encountered while writing the
encryption header to the underlying output stream.public PassphraseEncryptedOutputStream(@NotNull PassphraseEncryptedStreamHeader header, @NotNull java.io.OutputStream wrappedOutputStream, boolean writeHeaderToStream) throws java.security.GeneralSecurityException, java.io.IOException
header
- The existing passphrase-encrypted stream header that contains
the details to use for the encryption. It must not be
null
, and it must have an associated secret key.wrappedOutputStream
- The output stream to which the encrypted data (optionally
preceded by a header with details about the encryption) will
be written. It must not be null
.writeHeaderToStream
- Indicates whether to write the generated
PassphraseEncryptedStreamHeader
to the provided
wrappedOutputStream
before any encrypted data so that
a PassphraseEncryptedInputStream
can read it to obtain
information necessary for decrypting the data. If this is
false
, then the getEncryptionHeader()
method
must be used to obtain the encryption header so that it can be
stored elsewhere and provided to the
PassphraseEncryptedInputStream
constructor.java.security.GeneralSecurityException
- If a problem is encountered while
initializing the encryption.java.io.IOException
- If a problem is encountered while writing the
encryption header to the underlying output stream.public void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
b
- The byte of data to be written. Only the least significant 8
bits of the value will be used, and the most significant 24 bits
will be ignored.java.io.IOException
- If a problem is encountered while encrypting the data
or writing to the underlying output stream.public void write(@NotNull byte[] b) throws java.io.IOException
write
in class java.io.OutputStream
b
- The array containing the data to be written. It must not be
null
. All bytes in the array will be written.java.io.IOException
- If a problem is encountered while encrypting the data
or writing to the underlying output stream.public void write(@NotNull byte[] b, int offset, int length) throws java.io.IOException
write
in class java.io.OutputStream
b
- The array containing the data to be written. It must not
be null
.offset
- The index in the array of the first byte to be written.
It must be greater than or equal to zero, and less than the
length of the provided array.length
- The number of bytes to be written. It must be greater than
or equal to zero, and the sum of the offset
and
length
values must be less than or equal to the
length of the provided array.java.io.IOException
- If a problem is encountered while encrypting the data
or writing to the underlying output stream.public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
- If a problem is encountered while flushing data to
the underlying output stream.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException
- If a problem is encountered while closing the stream.@NotNull public PassphraseEncryptedStreamHeader getEncryptionHeader()
PassphraseEncryptedInputStream
constructor.