# Class Key

java.lang.Object
com.codename1.security.Key

Direct Known Subclasses:
  `PrivateKey, PublicKey, SecretKey`

public abstract class Key extends java.lang.Object

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.

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `Key(String algorithm, byte[] encoded, String format)`

  ###### Parameters

- ## Method Summary

  Modifier and Type
  Method
  Description
  `final String`
  `getAlgorithm()`
  Returns the algorithm this key is intended for (e.g. "AES", "RSA").
  `final byte[]`
  `getEncoded()`
  Returns a fresh copy of the encoded key bytes.
  `final String`
  `getFormat()`
  Returns the encoding format identifier.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### Key

    protected Key(String algorithm, byte[] encoded, String format)

    ###### Parameters

    - `algorithm`: human-readable algorithm name (e.g. "AES")

    - `encoded`: raw key material -- defensively copied

    - `format`: 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`)