Class 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(String), which
strips the armor and decodes the base64 for you, and which also takes the
-----BEGIN CERTIFICATE----- file a server or gateway is more likely to
hand out; fromX509(String, byte[]) is the lower level entry point for callers that
already hold the DER bytes.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic PublicKeyfromPem(byte[] pem) fromPem(String)over the raw bytes of a.pemfile, so a stream read withUtil.readInputStreamcan be passed straight in.static PublicKeyParses a PEM-encoded public key, determining the algorithm from the key itself.static PublicKeyfromPem(String,String)over the raw bytes of a.pemfile.static PublicKeyfromPem(String)with the algorithm supplied by the caller rather than read from the key.static PublicKeyWraps an X.509 / SubjectPublicKeyInfo (SPKI) DER blob.static PublicKeyrsa(byte[] x509Der) Convenience: build an RSAPublicKeyfrom afromX509(String, byte[])X.509 blob.Methods inherited from class Key
getAlgorithm, getEncoded, getFormat
-
Field Details
-
RSA
-
EC
-
-
Method Details
-
fromX509
-
rsa
Convenience: build an RSAPublicKeyfrom afromX509(String, byte[])X.509 blob. -
fromPem
Parses a PEM-encoded public key, determining the algorithm from the key itself. This is the form
openssl rsa -puboutand 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#1RSA PUBLIC KEYblock, 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.A
CERTIFICATEblock is accepted as well, and its subject public key is used. That covers the file a backend usually hands out -- a TLS or signing certificate rather than a bare key -- without the caller having to runopenssl x509 -pubkey -nooutfirst. In a certificate chain the leaf comes first, so the key that comes out is the chain's own. Note that nothing is being trusted by doing this: the signature and the validity dates are not checked, and the certificate is read only as a container for the key it names.Throws
CryptoExceptionif the text is not a public key, if it is passphrase-encrypted, or if the key is neither RSA nor EC. -
fromPem
fromPem(String)over the raw bytes of a.pemfile, so a stream read withUtil.readInputStreamcan be passed straight in. The bytes are decoded as UTF-8. -
fromPem
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
fromPem(String,String)over the raw bytes of a.pemfile.
-