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
PublicKeyis used to verify a signature (seeinitVerify()in theSignatureclass).-
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access