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.

public static final int ENCRYPT_MODE<br />

public static final int DECRYPT_MODE<br />

10.5.1.2 Key<br />

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

The key is an instance of the java.security.Key interface that's used to either encrypt or<br />

decrypt the data. Symmetric ciphers like DES use the same key for both encryption and<br />

decryption. Asymmetric ciphers like RSA use different keys for encryption or decryption.<br />

Keys are generally dependent on the cipher. For instance, an RSA key cannot be used to<br />

encrypt a DES file or vice versa. If the key you provide doesn't match the cipher's algorithm,<br />

an InvalidKeyException is thrown.<br />

To create a key, you first use the bytes of the key to construct a KeySpec for the algorithm<br />

you're using. Key specs are instances of the java.security.spec.KeySpec interface.<br />

Algorithm-specific implementations in the java.security.spec package include<br />

EncodedKeySpec, X509EncodedKeySpec, PKCS8EncodedKeySpec, DSAPrivateKeySpec, and<br />

DSAPublicKeySpec. Algorithm-specific implementations in the javax.crypto.spec<br />

package include DESKeySpec, DESedeKeySpec, DHPrivateKeySpec, DHPublicKeySpec,<br />

PBEKeySpec, RSAPrivateKeyCrtSpec, RSAPrivateKeySpec, and RSAPublicKeySpec. For<br />

example, if password is a string whose bytes are to form a DES key, the following creates a<br />

DESKeySpec object that can be used to encrypt or decrypt:<br />

byte[] desKeyData = password.getBytes();<br />

DESKeySpec desKeySpec = new DESKeySpec(desKeyData);<br />

Once you've constructed a key specification from the raw bytes of the key, you use a key<br />

factory to generate the actual key. A key factory is normally an instance of an algorithmspecific<br />

subclass of java.security.KeyFactory. It's retrieved by passing the name of the<br />

algorithm to the factory method javax.crypto.SecretKeyFactory.getInstance(). For<br />

example:<br />

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");<br />

SecretKey desKey = keyFactory.generateSecret(desKeySpec);<br />

Providers should supply the necessary key factories and spec classes for any algorithms they<br />

implement.<br />

A few algorithms, most notably Blowfish, use raw bytes as a key without any further<br />

manipulations. In these cases there may not be a key factory for the algorithm. Instead, you<br />

simply use the key spec as the secret key. For example:<br />

byte[] blowfishKeyData = password.getBytes();<br />

SecretKeySpec blowfishKeySpec = new SecretKeySpec(blowfishKeyData,<br />

"Blowfish");<br />

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

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

Most of the examples in this book use very basic and not particularly secure passwords as<br />

keys. Stronger encryption requires more random keys. The javax.crypto.KeyGenerator<br />

class provides methods that generate random keys for any installed algorithm. For example:<br />

KeyGenerator blowfishKeyGenerator = KeyGenerator.getInstance("Blowfish");<br />

222

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

Saved successfully!

Ooh no, something went wrong!