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.

SecretKey blowfishKey = blowfishKeyGenerator.generateKey();<br />

Cipher blowfish = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");<br />

blowfish.init(Cipher.ENCRYPT_MODE, blowfishKey);<br />

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

Generating random keys opens up the issue of how one stores and transmits the secret keys.<br />

To my way of thinking, random key generation makes more sense in public key cryptography,<br />

where all keys that need to be transmitted can be transmitted in the clear.<br />

10.5.1.3 Algorithm parameters<br />

The third possible argument to init() is a series of instructions for the cipher contained in an<br />

instance of the java.security.spec.AlgorithmParameterSpec interface or an instance of<br />

the java.security.AlgorithmParameters class. The AlgorithmParameterSpec interface<br />

declares no methods or constants. It's simply a marker for more specific subclasses that can<br />

provide additional, algorithm-dependent parameters for specific algorithms and modes (for<br />

instance, an initialization vector). If the algorithm parameters you provide don't fit the cipher's<br />

algorithm, an InvalidAlgorithmParameterException is thrown. The JCE provides several<br />

AlgorithmParameterSpec classes in the javax.crypto.spec package, including<br />

IVParameterSpec, which can set an initialization vector for modes that need it (CBC, CFB,<br />

and OFB), and PBEParameterSpec for password-based encryption.<br />

10.5.1.4 Source of randomness<br />

The final possible argument to init() is a SecureRandom object. This argument is only used<br />

when in encryption mode. This is an instance of the java.security.SecureRandom class, a<br />

subclass of java.util.Random that uses a pseudo-random number algorithm based on the<br />

SHA-1 hash algorithm instead of java.util.Random's linear congruential formula.<br />

java.util.Random's random numbers aren't random enough for strong cryptography. In this<br />

book, I will simply accept the default source of randomness.<br />

10.5.2 update( )<br />

Once the init() method has prepared the cipher for use, the update() method feeds data<br />

into it, encrypting or decrypting as it goes. There are four overloaded variants of this method.<br />

The first two return the encrypted or decrypted bytes:<br />

public final byte[] update(byte[] input) throws IllegalStateException<br />

public final byte[] update(byte[] input, int inputOffset, int inputLength)<br />

throws IllegalStateException<br />

These may return null if you're using a block cipher and not enough data has been provided<br />

to fill a block. The input data to be encrypted or decrypted is passed in as an array of bytes.<br />

Optional offsets and lengths may be used to select a particular subarray to be processed.<br />

update() throws an IllegalStateException if the cipher has not been initialized or it has<br />

already been finished with doFinal(). In either case, it's not prepared to accept data until<br />

init() is called.<br />

The second two variants of update() store the output in a buffer byte array passed in as the<br />

fourth argument and return the number of bytes stored in the buffer:<br />

223

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

Saved successfully!

Ooh no, something went wrong!