public final class PublicKey

  1. Object
  2. Key
  3. PublicKey

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, which strips the armor and decodes the base64 for you; fromX509 is the lower level entry point for callers that already hold the DER bytes.

Fields

public static final String RSA = "RSA"RSA algorithm identifier (“RSA”).
public static final String EC = "EC"Elliptic-curve algorithm identifier (“EC”).

Methods

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

Inherited methods

Field details

RSA

public static final String RSA = "RSA"
RSA algorithm identifier (“RSA”).

EC

public static final String EC = "EC"
Elliptic-curve algorithm identifier (“EC”).

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 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 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 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.