Keys

The java.security.Key interface encapsulates a cryptographic key. It defines only three methods:

public String getAlgorithm()

This method returns the name of the cryptographic algorithm for which this key is used. An example is DSA, the Digital Signature Algorithm.

public byte[] getEncoded()

You can retrieve the encoded value of the key by calling this method. Encoding is a process of mapping the key’s value into an array of bytes. The getFormat() method will return the name of the format used to encode the key.

public String getFormat()

This method returns the name of the format used to encode the key. An example is X.509.

Several interfaces extend the Key interface. These child interfaces define different flavors of keys, but none of them defines any additional methods; they are used for clarity and type safety. As we saw in Chapter 2, keys are used differently for symmetric ciphers, asymmetric ciphers, and signatures. Semantic extensions to the Key interfaces keep the concepts clear. In the JDK, there are two such interfaces:

java.security.PublicKey

This interface represents the public key of a key pair, suitable for use with a signature or an asymmetric cipher (see Chapter 6, and Chapter 7, respectively). When used with a signature, a PublicKey is used to verify a signature (see initVerify() in the Signature class).

java.security.PrivateKey

This interface represents the other half of a key pair. Just like a public key, a private key can be used with a signature or an ...

Get Java Cryptography now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.