public class JcaPrivateKeyReader
extends java.lang.Object
PrivateKey, transparently decrypting the password-protected variants.
The following encodings are recognised, on the PEM side by the type PEMParser
hands back and on the DER side by the structure of the outermost SEQUENCE:
-----BEGIN RSA PRIVATE KEY----- and their DER body;PrivateKeyInfo - -----BEGIN PRIVATE KEY----- and DER;-----BEGIN ... PRIVATE KEY----- with a
Proc-Type/DEK-Info header), decrypted with the supplied password;-----BEGIN ENCRYPTED PRIVATE KEY----- and DER
EncryptedPrivateKeyInfo), decrypted with the supplied password.
This is the JCA-aware read-side companion to JcaPKIXIdentityBuilder and lives in the
.jcajce package because it produces a PrivateKey. It is a
convenience over PEMParser / JcaPEMKeyConverter only - the standards-compliant
writers (JcaPKCS8Generator, JcePEMEncryptorBuilder,
JceOpenSSLPKCS8EncryptorBuilder) remain the way to emit keys.
| Constructor and Description |
|---|
JcaPrivateKeyReader()
Base constructor for reading unencrypted keys.
|
JcaPrivateKeyReader(char[] password)
Construct a reader carrying the password used to decrypt password-protected keys.
|
| Modifier and Type | Method and Description |
|---|---|
java.security.PrivateKey |
readKey(byte[] encoding)
Read a private key from a buffer of PEM or DER bytes, auto-detecting the encoding.
|
java.security.PrivateKey |
readKey(java.io.File keyFile)
Read a private key from a PEM or DER file, auto-detecting the encoding.
|
java.security.PrivateKey |
readKey(java.io.InputStream keyStream)
Read a private key from a stream of PEM or DER bytes, auto-detecting the encoding.
|
java.security.PrivateKey |
readKey(java.io.Reader reader)
Read a private key from a PEM reader.
|
JcaPrivateKeyReader |
setProvider(java.security.Provider provider) |
JcaPrivateKeyReader |
setProvider(java.lang.String providerName) |
public JcaPrivateKeyReader()
PEMException.public JcaPrivateKeyReader(char[] password)
password - the password to decrypt with, or null if no encrypted key is expected.public JcaPrivateKeyReader setProvider(java.security.Provider provider)
public JcaPrivateKeyReader setProvider(java.lang.String providerName)
public java.security.PrivateKey readKey(java.io.File keyFile)
throws java.io.IOException
keyFile - the file containing the key.java.io.IOException - on a read, parse, or decryption failure.public java.security.PrivateKey readKey(java.io.InputStream keyStream)
throws java.io.IOException
keyStream - the stream containing the key.java.io.IOException - on a read, parse, or decryption failure.public java.security.PrivateKey readKey(byte[] encoding)
throws java.io.IOException
encoding - the key bytes.java.io.IOException - on a parse or decryption failure.public java.security.PrivateKey readKey(java.io.Reader reader)
throws java.io.IOException
reader - a reader positioned at a PEM private key object.java.io.IOException - on a parse or decryption failure.