The Architecture of Engine Classes

programmers as opaque objects. Well show their complete interface which you might be curious about and which is absolutely needed if youre writing your own security provider, but well try not to lose sight of the two goals of this chapter.

9.1 Keys

Lets start with the various classes that support the notion of keys within Java.

9.1.1 The Key Interface

The concept of a key is modeled by the Key interface java.security.Key : public interface Key extends Serializable Model the concept of a single key. Because keys must be transferred to and from various entities, all keys must be serializable. As we discussed in Chapter 8, there might be several algorithms available for generating and understanding keys, depending on the particular security providers that are installed in the virtual machine. Hence, the first thing a key needs to be able to tell us is what algorithm generated it: public String getAlgorithm Return a string describing the algorithm used to generate this key; this string should be the name of a standard key generation algorithm. When a key is transferred between two parties, it is usually encoded as a series of bytes; this encoding must follow a format defined for the type of key. Keys are not required to support encoding −− in which case the format of the data transferred between the two parties in a key exchange is either obvious e.g., simply the serialized data of the key or specific to a particular implementation. Keys tell us the format they use for encoding their output with this method: public String getFormat Return a string describing the format of the encoding the key supports. The encoded data of the key itself is produced by this method: public byte[] getEncoded Return the bytes that make up the particular key in the encoding format the key supports. The encoded bytes are the external representation of the key in binary format. Those are the only methods that a key is guaranteed to implement other than methods of the Object class, of course; most implementations of keys override many of those methods. In particular, youll note that there is nothing in the key interface that says anything about decoding a key. Well say more about that later.

9.1.2 Asymmetric Keys

Asymmetric keys are the more popular type of key. These keys come in pairs; hence the core Java API contains these two additional interfaces: public interface PublicKey extends Key 140