public abstract class Key
- Object
- Key
Known subtypesPrivateKey, PublicKey, SecretKey
Common base for every key type in the security package – SecretKey for
symmetric algorithms, PublicKey and PrivateKey for asymmetric ones.
Holds the three pieces of metadata every key carries:
- an algorithm name (e.g. “AES”, “RSA”, “EC”)
- a defensive copy of the encoded key bytes
- a format identifier (e.g. “RAW” for symmetric keys, “X.509” for SPKI public keys, “PKCS#8” for private keys)
Application code should not extend this class directly; create the concrete subtype that matches the algorithm you are using.
Constructors
protected Key(String algorithm, byte[] encoded, String format) |
Methods
public final byte[] getEncoded() | Returns a fresh copy of the encoded key bytes. |
public final String getAlgorithm() | Returns the algorithm this key is intended for (e.g. “AES”, “RSA”). |
public final String getFormat() | Returns the encoding format identifier. |
Inherited methods
Constructor details
Key
protected Key(String algorithm, byte[] encoded, String format)Parameters
algorithmString- human-readable algorithm name (e.g. “AES”)
encodedbyte[]- raw key material – defensively copied
formatString- encoding format identifier (“RAW”, “X.509”, “PKCS#8”)
Method details
getEncoded
public final byte[] getEncoded()Returns a fresh copy of the encoded key bytes. Treat returns from
private keys as sensitive material – do not log or store
unencrypted.
getAlgorithm
public final String getAlgorithm()Returns the algorithm this key is intended for (e.g. “AES”, “RSA”).
getFormat
public final String getFormat()Returns the encoding format identifier. Standard values:
- “RAW” – symmetric keys (
SecretKey) - “X.509” – SubjectPublicKeyInfo DER (
PublicKey) - “PKCS#8” – PrivateKeyInfo DER (
PrivateKey)