# Class PublicKey

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

public final class PublicKey extends Key

A public key -- paired with a `PrivateKey` to form a key pair. Carries the algorithm name ("RSA" or "EC") and the encoded key bytes.

PEM files (`-----BEGIN PUBLIC KEY-----`) go through `fromPem(String)`, which strips the armor and decodes the base64 for you; `fromX509(String, byte[])` is the lower level entry point for callers that already hold the DER bytes.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `EC`
  Elliptic-curve algorithm identifier ("EC").
  `static final String`
  `RSA`
  RSA algorithm identifier ("RSA").

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static PublicKey`
  `fromPem(byte[] pem)`
  `fromPem(String)` over the raw bytes of a `.pem` file, so a stream read with `Util.readInputStream` can be passed straight in.
  `static PublicKey`
  `fromPem(String pem)`
  Parses a PEM-encoded public key, determining the algorithm from the key itself.
  `static PublicKey`
  `fromPem(String algorithm, byte[] pem)`
  `fromPem(String,String)` over the raw bytes of a `.pem` file.
  `static PublicKey`
  `fromPem(String algorithm, String pem)`
  `fromPem(String)` with the algorithm supplied by the caller rather than read from the key.
  `static PublicKey`
  `fromX509(String algorithm, byte[] x509Der)`
  Wraps an X.509 / SubjectPublicKeyInfo (SPKI) DER blob.
  `static PublicKey`
  `rsa(byte[] x509Der)`
  Convenience: build an RSA `PublicKey` from a `fromX509(String, byte[])` X.509 blob.

  ### Methods inherited from class Key

  `getAlgorithm, getEncoded, getFormat`

  ### Methods inherited from class Object

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

- ## Field Details

  - ### RSA

    public static final String RSA
    RSA algorithm identifier ("RSA").
    See Also:
      - Constant Field Values

  - ### EC

    public static final String EC
    Elliptic-curve algorithm identifier ("EC").
    See Also:
      - Constant Field Values

- ## Method Details

  - ### fromX509

    public static PublicKey fromX509(String algorithm, byte[] x509Der)
    Wraps an X.509 / SubjectPublicKeyInfo (SPKI) DER blob. This is the format produced by `openssl rsa -pubout` or `openssl ec -pubout`.

  - ### rsa

    public static PublicKey rsa(byte[] x509Der)
    Convenience: build an RSA `PublicKey` from a `fromX509(String, byte[])` X.509 blob.

  - ### fromPem

    public static PublicKey fromPem(String pem)

    Parses a PEM-encoded public key, determining the algorithm from the key itself. This is the form `openssl rsa -pubout` and every backend key store hands out:

        InputStream is = Display.getInstance().getResourceAsStream(MyApp.class, "/public.pem");
        PublicKey key = PublicKey.fromPem(Util.readInputStream(is));

    Accepts a `PUBLIC KEY` (SPKI) block, the older PKCS#1 `RSA PUBLIC KEY` block, and -- for keys carried in JSON or a build hint rather than a file -- bare base64 with no `-----BEGIN-----` armor at all. Line endings, blank lines and text surrounding the block are ignored, and in a file holding several blocks the first public key is the one used.

    Throws `CryptoException` if the text is not a public key, if it is passphrase-encrypted, or if the key is neither RSA nor EC.

  - ### fromPem

    public static PublicKey fromPem(byte[] pem)
    `fromPem(String)` over the raw bytes of a `.pem` file, so a stream read with `Util.readInputStream` can be passed straight in. The bytes are decoded as UTF-8.

  - ### fromPem

    public static PublicKey fromPem(String algorithm, String pem)
    `fromPem(String)` with the algorithm supplied by the caller rather than read from the key. Use this only for a key whose algorithm OID this class does not recognize but the platform does.

  - ### fromPem

    public static PublicKey fromPem(String algorithm, byte[] pem)
    `fromPem(String,String)` over the raw bytes of a `.pem` file.