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:
- Managing existing keys. This is done through the Identity
classes, which are used to hold, index and manage keys.
- Generating new keys. This is done through classes that generate
keys, such as Signature.
- Constructing a key from encoded data. This is useful when
importing a key encoded in a specific format, such as X.509,
PKCS#8, or PGP, and is done using Key class constructors.
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
-
Key()
- Constructs an uninitialized key for serialization.
-
Key(byte[], String)
- Constructs a key, specifying the encoded key and the encoding
format.
-
Key(byte[], String, String)
- Constructs a key, specifying the encoded key, the encoding
format, and the algorithm name.
-
equals(Object)
- Returns true if the canonical encoded form of this key and the
argument key is the same.
-
getAlgorithm()
- Returns the standard algorithm name this key is for.
-
getEncoded()
- Returns the encoded key.
-
getFormat()
- Returns the format used to encode the key.
-
initialize(byte[], String, String)
- Initializes this key object with the specified encoded key,
format, and algorithm name.
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.
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.
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.
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.
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.
getFormat
public final String getFormat()
- Returns the format used to encode the key.
- Returns:
- the format used to encode the key.
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.
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