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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

A CipherInputStream object contains a Cipher object that's used to decrypt or encrypt all<br />

data read from the underlying stream before passing it to the eventual source. This Cipher<br />

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

another input stream as an argument:<br />

public CipherInputStream(InputStream in, 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(). This Cipher object<br />

must also have been initialized for either encryption or decryption with init() before being<br />

passed into the constructor. There is also a protected constructor that might be used by<br />

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

protected CipherInputStream(InputStream in)<br />

CipherInputStream overrides most methods declared in FilterInputStream. Each of these<br />

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

number of bytes after encryption or decryption, which is important if the ciphertext does not<br />

have the same length as the plaintext. The available() method also returns the number of<br />

bytes available after encryption or decryption. The markSupported() method returns false;<br />

you cannot mark and reset a cipher input stream, even if the underlying class supports<br />

marking and resetting. Allowing this would confuse many encryption algorithms. However,<br />

you can make a cipher input stream the underlying stream of another class like<br />

BufferedInputStream, which does support marking and resetting.<br />

Strong encryption schemes have the distinct disadvantage that changing even a single bit in<br />

the data can render the entire file unrecoverable gibberish. Therefore, it's useful to combine<br />

encryption with a digest so you can tell whether a file has been modified. Example 10.8 uses<br />

CipherInputStream to DES-encrypt a file named on the command line, but that's not all. The<br />

ciphertext is also digested and the digest saved so corruption can be detected.<br />

Example 10.8. DigestEncryptor<br />

import java.io.*;<br />

import java.security.*;<br />

import java.security.spec.*;<br />

import javax.crypto.*;<br />

import javax.crypto.spec.*;<br />

public class DigestEncryptor {<br />

public static void main(String[] args) {<br />

if (args.length != 2) {<br />

System.err.println("Usage: java DigestEncryptor filename password");<br />

return;<br />

}<br />

String filename = args[0];<br />

String password = args[1];<br />

227

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

Saved successfully!

Ooh no, something went wrong!