All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.security.Key

java.lang.Object
   |
   +----java.security.Key

public class Key
extends Object
implements Serializable

This class is used for representing a cryptographic key. Keys are opaque containers, which hold an encoded key, along with the encoding format and the name of the algorithm the key is for.

Applications use keys in three ways:

The Key class itself is abstract and is subclassed by key type (such as PublicKey and PrivateKey), and should be subclassed further to provide specialized functionality to parse specific formats and implement algorithm-specific behaviors.

JavaSecurity supports cross-provider interoperability. It ensures interoperability through the use of well-defined, standard key encoding. Consistent encoding of keys enables an implementation from a given provider to work with the keys generated by another provider. One level of security is maintained by the Java Virtual Machine by ensuring that only registered and authorized providers have access to secret key data.

The Key class also supports providers which opt to be non-interoperable either by choice or by necessity: for example if the provider uses hardware-based key storage, it may not be possible or desirable to extract the key data and hand it over to another registered and authorized provider. Instead, such a provider would represent a software key using an internal reference (such as a memory address), meaningful to that provider only.

Keys are immutable, that is, once they have been created they may not be changed. It is the responsibily of subclasses to ensure that this property is maintained.

See Also:
PublicKey, PrivateKey, KeyParams, Identity, IdentityScope, Signer

Constructor Index

 o Key()
Constructs an uninitialized key for serialization.
 o Key(byte[], String)
Constructs a key, specifying the encoded key and the encoding format.
 o Key(byte[], String, String)
Constructs a key, specifying the encoded key, the encoding format, and the algorithm name.

Method Index

 o equals(Object)
Returns true if the canonical encoded form of this key and the argument key is the same.
 o getAlgorithm()
Returns the standard algorithm name this key is for.
 o getEncoded()
Returns the encoded key.
 o getFormat()
Returns the format used to encode the key.
 o initialize(byte[], String, String)
Initializes this key object with the specified encoded key, format, and algorithm name.

Constructors

 o Key
  protected Key()
Constructs an uninitialized key for serialization. Subclasses using this constructor are responsible for maintaining the object's invariance, and will typically call initialize from within the constructor.

 o Key
  public Key(byte encodedKey[],
             String format)
Constructs a key, specifying the encoded key and the encoding format. This constructor is used when the algorithm is unknown.

Parameters:
encodedKey - the key, encoded using format.
format - the format used to encode the key.
 o Key
  public Key(byte encodedKey[],
             String format,
             String algorithm)
Constructs a key, specifying the encoded key, the encoding format, and the algorithm name. See algorithm names for information about standard algorithm names.

Parameters:
encodedKey - the key, encoded using format.
format - the format used to encode the key.
algorithm - the name of the algorithm this key is for.

Methods

 o initialize
  protected final void initialize(byte encodedKey[],
                                  String format,
                                  String algorithm)
Initializes this key object with the specified encoded key, format, and algorithm name. This method should be called by subclasses unable to invoke super with the proper arguments at construction time. See algorithm names for information about standard algorithm names.

Parameters:
encodedKey - the key, encoded using format.
format - the format used to encode the key.
algorithm - the name of the algorithm this key is for.
 o getAlgorithm
  public final String getAlgorithm()
Returns the standard algorithm name this key is for. For example, "DSA" would indicate that this key is a DSA key. This is further typed by the key subclass, for example if the subclass is PublicKey, this indicates that the key is a DSA public key. Note that this method may return null, when the algorithm this key is for is unknown. See algorithm names for information about standard algorithm names.

Returns:
the name of the algorithm this key is for.
 o getFormat
  public final String getFormat()
Returns the format used to encode the key.

Returns:
the format used to encode the key.
 o getEncoded
  protected byte[] getEncoded() throws InvalidKeyException
Returns the encoded key.

Returns:
the encoded key.
Throws: InvalidKeyException
if the key cannot be encoded, for example if the original encoding was invalid, or if the key was not properly initialized.
 o equals
  public final boolean equals(Object obj)
Returns true if the canonical encoded form of this key and the argument key is the same.

Returns:
true if the two keys share the same encoding, the same format and the same algorithm (or if one does not have its algorithm initialized).
Overrides:
equals in class Object

All Packages  Class Hierarchy  This Package  Previous  Next  Index