public class EncryptedAttachmentOutputStream
extends java.io.FilterOutputStream
An EncryptedAttachmentOutputStream handles writing AES encrypted data to disk, combining the encrypted data with a header specifying the IV used during encryption. A CipherOutputStream is used to encrypt the data sent via write() methods.
This class adheres strictly to the semantics, especially the failure semantics, of its ancestor classes java.io.OutputStream and java.io.FilterOutputStream. This class has exactly those methods specified in its ancestor classes, and overrides them all. Moreover, this class catches all exceptions that are not thrown by its ancestor classes.
It is crucial for a programmer using this class not to use methods that are not defined or overriden in this class (such as a new method or constructor that is later added to one of the super classes), because the design and implementation of those methods are unlikely to have considered security impact with regard to the underlying CipherOutputStream.
The data format written by this class is read by EncryptedAttachmentInputStream, and is:
Header: 1-byte : version number, 1 16-byte: initialisation vector Body: AES CBC Encrypted file content.
Constructor and Description |
---|
EncryptedAttachmentOutputStream(java.io.OutputStream out,
byte[] key,
byte[] iv)
Creates an output stream with a key and IV.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
flush() |
void |
write(byte[] b) |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
public EncryptedAttachmentOutputStream(java.io.OutputStream out, byte[] key, byte[] iv) throws java.security.InvalidAlgorithmParameterException, java.security.InvalidKeyException, java.io.IOException
Creates an output stream with a key and IV.
This constructor writes bytes to the out parameter in order to write the header.
Note: if the specified output stream is null, a NullPointerException is thrown as the constructor tries to write to the passed output stream.
out
- the output stream object.key
- the encryption key to use. Length must be supported by underlying
JCE implementation.iv
- the initialisation vector to use.java.security.InvalidAlgorithmParameterException
- if IV is wrong sizejava.security.InvalidKeyException
- if key is wrong sizejava.io.IOException
- on I/O exceptionspublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.FilterOutputStream
java.io.IOException
public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.FilterOutputStream
java.io.IOException
public void write(byte[] b) throws java.io.IOException
write
in class java.io.FilterOutputStream
java.io.IOException
public void write(byte[] b, int off, int len) throws java.io.IOException
write
in class java.io.FilterOutputStream
java.io.IOException
public void write(int b) throws java.io.IOException
write
in class java.io.FilterOutputStream
java.io.IOException