23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

10.6.2 CipherOutputStream<br />

CipherOutputStream is a subclass of FilterOutputStream.<br />

public class CipherOutputStream extends FilterOutputStream<br />

<strong>Java</strong> I/O<br />

Each CipherOutputStream object contains a Cipher object used to decrypt or encrypt all<br />

data passed as arguments to the write() method before writing it to the underlying stream.<br />

This Cipher object is set in the constructor. Like all filter stream constructors, this constructor<br />

takes another input stream as an argument:<br />

public CipherOutputStream(OutputStream out, Cipher c)<br />

The Cipher object used here must be a properly initialized instance of<br />

javax.crypto.Cipher, most likely returned by Cipher.getInstance(). The Cipher object<br />

c should be initialized for encryption or decryption by calling init() before being passed to<br />

the CipherOutputStream() constructor. There is also a protected constructor that might be<br />

used by subclasses that want to implement their own, non-JCE-based encryption scheme:<br />

protected CipherOutputStream(OutputStream out)<br />

CipherOutputStream has all the usual methods of any output stream, like write(),<br />

flush(), and close(). It overrides five of these methods to do its filtering:<br />

public void write(int b) throws <strong>IO</strong>Exception<br />

public void write(byte[] data) throws <strong>IO</strong>Exception<br />

public void write(byte[] data, int offset, int length) throws <strong>IO</strong>Exception<br />

public void flush() throws <strong>IO</strong>Exception<br />

public void close() throws <strong>IO</strong>Exception<br />

Clients use these methods the same way they use them in any output stream. Before the data<br />

is written, the stream's cipher either decrypts or encrypts the data. Each of these five methods<br />

makes the necessary adjustments to handle encrypted data. For example, the flush() method<br />

(which is invoked by the close() method as well) calls doFinal() on the Cipher object to<br />

make sure it has finished padding and encrypting all the data before it flushes the final data to<br />

the underlying stream.<br />

There are no new methods in CipherOutputStream not declared in the superclass. Anything<br />

else you need to do, such as getting the cipher's initialization vector, must be handled by the<br />

Cipher object.<br />

Example 10.9 uses CipherOutputStream to decrypt files encrypted by the DigestEncryptor<br />

of Example 10.8. A digest input stream chained to a file input stream checks the digest of the<br />

ciphertext as it's read from the file. If the digest does not match, an error message is printed.<br />

The file is still written into the output file, since—depending on the algorithm and mode<br />

used—it may be partially legible, especially if the error does not occur until relatively late in<br />

the encrypted data.<br />

229

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!