The Key Interface Keys

Model a data object that contains a public key and a private key. The KeyPair class is a very simple data structure class containing two pieces of information: a public key and a private key. When we need to generate our own keys which well do next, well need to generate both the public and private key at once. This object will contain both of the necessary keys. If youre not interested in generating your own keys, this class may be ignored. The KeyPair class contains only two methods: public PublicKey getPublic public PrivateKey getPrivate Return the desired key from the key pair. A key pair object is instantiated through a single constructor: public KeyPairPublicKey pub, PrivateKey priv Create a key pair object, initializing each member of the pair. In theory, a key pair should not be initialized without both members of the pair being present; there is nothing, however, that prevents us from passing null as one of the keys. Similarly, there are no security provisions within the KeyPair class that prevent the private key from being accessed −− no calls to the security manager are made when the getPrivate method is invoked. Hence the KeyPair class should be used with caution.

9.1.3 Symmetric Keys

Symmetric or secret keys are used only within JCE, where they are defined by the SecretKey interface javax.crypto.SecretKey : public interface SecretKey extends Key Identify a class as being a symmetric key. Secret keys have no specific identifying information; this interface is empty and is used only for type identification. There are many types of secret keys used within JCE: Blowfish, DES, DESede, HmacMD5, HmacSHA1, PBEWithMD5AndDES, and PBEWithMD5AndTripleDES. Unlike asymmetric keys, however, these different key types do not have their own interfaces.

9.2 Generating Keys

Javas security API provides two standard engines to generate keys: one to generate a pair of asymmetric keys and one to generate a secret key.

9.2.1 The KeyPairGenerator Class

Generation of public and private keys is provided by the KeyPairGenerator class java.security.KeyPairGenerator : public abstract class KeyPairGenerator extends KeyPairGeneratorSpi Generate and provide information about publicprivate key pairs. Chapter 9. Keys and Certificates