public class

X509Key

extends Object
implements PublicKey
java.lang.Object
   ↳ sun.security.x509.X509Key
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

Holds an X.509 key, for example a public key found in an X.509 certificate. Includes a description of the algorithm to be used with the key; these keys normally are used as "SubjectPublicKeyInfo".

While this class can represent any kind of X.509 key, it may be desirable to provide subclasses which understand how to parse keying data. For example, RSA public keys have two members, one for the public modulus and one for the prime exponent. If such a class is provided, it is used when parsing X.509 keys. If one is not provided, the key still parses correctly.

Summary

[Expand]
Inherited Constants
From interface java.security.Key
From interface java.security.PublicKey
Fields
protected AlgorithmId algid
protected byte[] encodedKey
protected byte[] key This field is deprecated. Use the BitArray form which does not require keys to be byte aligned.
Public Constructors
X509Key()
Default constructor.
Public Methods
void decode(InputStream in)
Initialize an X509Key object from an input stream.
void decode(byte[] encodedKey)
byte[] encode()
Returns the DER-encoded form of the key as a byte array.
final void encode(DerOutputStream out)
Encode SubjectPublicKeyInfo sequence on the DER output stream.
boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
String getAlgorithm()
Returns the algorithm to be used with this key.
AlgorithmId getAlgorithmId()
Returns the algorithm ID to be used with this key.
byte[] getEncoded()
Returns the DER-encoded form of the key as a byte array.
byte[] getEncodedInternal()
String getFormat()
Returns the format for this key: "X.509"
int hashCode()
Calculates a hash code value for the object.
static PublicKey parse(DerValue in)
Construct X.509 subject public key from a DER value.
String toString()
Returns a string representation of the object.
Protected Methods
BitArray getKey()
Gets the key.
void parseKeyBits()
Parse the key bits.
void setKey(BitArray key)
Sets the key in the BitArray form.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.security.Key

Fields

protected AlgorithmId algid

protected byte[] encodedKey

protected byte[] key

This field is deprecated.
Use the BitArray form which does not require keys to be byte aligned.

The key bytes, without the algorithm information.

Public Constructors

public X509Key ()

Default constructor. The key constructed must have its key and algorithm initialized before it may be used, for example by using decode.

Public Methods

public void decode (InputStream in)

Initialize an X509Key object from an input stream. The data on that input stream must be encoded using DER, obeying the X.509 SubjectPublicKeyInfo format. That is, the data is a sequence consisting of an algorithm ID and a bit string which holds the key. (That bit string is often used to encapsulate another DER encoded sequence.)

Subclasses should not normally redefine this method; they should instead provide a parseKeyBits method to parse any fields inside the key member.

The exception to this rule is that since private keys need not be encoded using the X.509 SubjectPublicKeyInfo format, private keys may override this method, encode, and of course getFormat.

Parameters
in an input stream with a DER-encoded X.509 SubjectPublicKeyInfo value
Throws
InvalidKeyException on parsing errors.

public void decode (byte[] encodedKey)

public byte[] encode ()

Returns the DER-encoded form of the key as a byte array.

Throws
InvalidKeyException on encoding errors.

public final void encode (DerOutputStream out)

Encode SubjectPublicKeyInfo sequence on the DER output stream.

Throws
IOException on encoding errors.

public boolean equals (Object obj)

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Parameters
obj the reference object with which to compare.
Returns
  • true if this object is the same as the obj argument; false otherwise.

public String getAlgorithm ()

Returns the algorithm to be used with this key.

Returns
  • the name of the algorithm associated with this key.

public AlgorithmId getAlgorithmId ()

Returns the algorithm ID to be used with this key.

public byte[] getEncoded ()

Returns the DER-encoded form of the key as a byte array.

Returns
  • the encoded key, or null if the key does not support encoding.

public byte[] getEncodedInternal ()

public String getFormat ()

Returns the format for this key: "X.509"

Returns
  • the primary encoding format of the key.

public int hashCode ()

Calculates a hash code value for the object. Objects which are equal will also have the same hashcode.

Returns
  • a hash code value for this object.

public static PublicKey parse (DerValue in)

Construct X.509 subject public key from a DER value. If the runtime environment is configured with a specific class for this kind of key, a subclass is returned. Otherwise, a generic X509Key object is returned.

This mechanism gurantees that keys (and algorithms) may be freely manipulated and transferred, without risk of losing information. Also, when a key (or algorithm) needs some special handling, that specific need can be accomodated.

Parameters
in the DER-encoded SubjectPublicKeyInfo value
Throws
IOException on data format errors

public String toString ()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns
  • a string representation of the object.

Protected Methods

protected BitArray getKey ()

Gets the key. The key may or may not be byte aligned.

Returns
  • a BitArray containing the key.

protected void parseKeyBits ()

Parse the key bits. This may be redefined by subclasses to take advantage of structure within the key. For example, RSA public keys encapsulate two unsigned integers (modulus and exponent) as DER values within the key bits; Diffie-Hellman and DSS/DSA keys encapsulate a single unsigned integer.

This function is called when creating X.509 SubjectPublicKeyInfo values using the X509Key member functions, such as parse and decode.

Throws
IOException on parsing errors.
InvalidKeyException on invalid key encodings.

protected void setKey (BitArray key)

Sets the key in the BitArray form.