Class PrivateKey
A private key -- paired with a PublicKey to form a key pair. Carries the
algorithm name ("RSA" or "EC") and the encoded key bytes.
PEM files (-----BEGIN PRIVATE KEY-----) go through fromPem(String), which
strips the armor and decodes the base64 for you; fromPkcs8(String, byte[]) is the lower
level entry point for callers that already hold the DER bytes.
-
Method Summary
Modifier and TypeMethodDescriptionstatic PrivateKeyfromPem(byte[] pem) fromPem(String)over the raw bytes of a.pemfile, so a stream read withUtil.readInputStreamcan be passed straight in.static PrivateKeyParses a PEM-encoded private key, determining the algorithm from the key itself:static PrivateKeyfromPem(String,String)over the raw bytes of a.pemfile.static PrivateKeyfromPem(String)with the algorithm supplied by the caller rather than read from the key.static PrivateKeyWraps a PKCS#8 DER blob.static PrivateKeyrsa(byte[] pkcs8Der) Convenience: build an RSAPrivateKeyfrom afromPkcs8(String, byte[])PKCS#8 blob.Methods inherited from class Key
getAlgorithm, getEncoded, getFormat
-
Method Details
-
fromPkcs8
Wraps a PKCS#8 DER blob. This is the format produced byopenssl pkcs8 -topk8 -nocrypt. -
rsa
Convenience: build an RSAPrivateKeyfrom afromPkcs8(String, byte[])PKCS#8 blob. -
fromPem
Parses a PEM-encoded private key, determining the algorithm from the key itself:
InputStream is = Display.getInstance().getResourceAsStream(MyApp.class, "/private.pem"); PrivateKey key = PrivateKey.fromPem(Util.readInputStream(is));Accepts a
PRIVATE KEY(PKCS#8) block and also the olderRSA PRIVATE KEY(PKCS#1) andEC PRIVATE KEY(SEC1) blocks, which are rewrapped as PKCS#8 here -- so a key straight out ofssh-keygen -m PEMoropenssl ecparam -genkeyworks without a conversion step, including theEC PARAMETERSblock that command writes ahead of the key: the first block that actually is a private key is the one used. Bare base64 with no-----BEGIN-----armor is accepted too.A passphrase-encrypted key (
ENCRYPTED PRIVATE KEY) is rejected with aCryptoExceptionnaming the command that decrypts it; so is a key that is neither RSA nor EC.The bytes behind a private key are sensitive -- do not log the result of
getEncoded(). -
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.
-