Preview
Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).
Programs can only use PEMDecoder when preview features are enabled.
Preview features may be removed in a future release,
or upgraded to permanent features of the Java platform.
PEMDecoder implements a decoder for Privacy-Enhanced Mail (PEM) data.
PEM is a textual encoding used to store and transfer cryptographic
objects, such as asymmetric keys, certificates, and certificate revocation
lists (CRLs). It is defined in RFC 1421 and RFC 7468. PEM consists of
Base64-encoded content enclosed by a header and footer that identify the
type of the content.
The decode(String) and decode(InputStream) methods
return an instance of a class that matches the PEM type and implements
BinaryEncodable, as follows:
X509CertificateX509CRLPublicKeyPrivateKey or KeyPair
(if the encoding contains a public key)EncryptedPrivateKeyInfoPEMPEMDecoder instance configured for decryption:
PrivateKey or KeyPair
(if the encoding contains a public key) If the PEM type has no corresponding class, decode(String) and
decode(InputStream) return a PEM object.
If application code switches over the BinaryEncodable result of
decode(String) or decode(InputStream), the switch cannot
be made exhaustive simply by providing a case label for every permitted
subtype listed for BinaryEncodable; there also must be a default
or case BinaryEncodable label to handle additional subtypes that
might be added in the future.
The decode(String, Class) and decode(InputStream, Class)
methods accept a parameter specifying the desired BinaryEncodable
result. These methods avoid the need for casting and are useful when multiple
representations are possible. For example, if the PEM contains both public and
private keys, specifying PrivateKey.class returns only the private key.
If X509EncodedKeySpec.class is provided, the public key encoding is
returned as a X509EncodedKeySpec. To retrieve a PEM object,
use PEM.class. If the specified class does not
match the PEM content, a ClassCastException is thrown.
In addition to the types listed above, these methods support the
following PEM types and BinaryEncodable classes when specified as
parameters:
X509EncodedKeySpecPKCS8EncodedKeySpecPublicKey (if the encoding contains a public key)X509EncodedKeySpec (if the encoding contains a public key)PEMDecoder instance configured for decryption:
PKCS8EncodedKeySpecPublicKey (if the encoding contains a public key)X509EncodedKeySpec (if the encoding contains a public key) A new PEMDecoder instance is created when configured
with withFactoriesOf(Provider) or withDecryption(char[]).
The withFactoriesOf(Provider) method uses the specified provider when
obtaining KeyFactory and CertificateFactory instances used
during decoding. The withDecryption(char[]) method configures the
decoder to decrypt and decode encrypted private key PEM data using the given
password. If decryption fails, a CryptoException is thrown.
If an encrypted PEM is processed by a decoder not configured
for decryption, an EncryptedPrivateKeyInfo is returned.
A PEMDecoder configured for decryption can also decode unencrypted PEM.
This class is immutable and thread-safe.
Example: decode a private key:
PEMDecoder pd = PEMDecoder.of(); PrivateKey priKey = pd.decode(priKeyPEM, PrivateKey.class);
Example: configure decryption and a factory provider:
PEMDecoder pd = PEMDecoder.of().withDecryption(password). withFactoriesOf(provider); BinaryEncodable pemData = pd.decode(privKeyPEM);
Implementation Note
This implementation decodes non-encrypted RSA PRIVATE KEY as PrivateKey,
X509 CERTIFICATE and X.509 CERTIFICATE as X509Certificate, and CRL as
X509CRL. Other implementations may recognize additional PEM types.
PEMEncoder, PEM, EncryptedPrivateKeyInfo, BinaryEncodable
| Modifier and Type | Field and Description |
|---|---|
| private final Provider | |
| private final PBEKeySpec | |
| private static final PEMDecoder |
| Access | Constructor and Description |
|---|---|
| private | PEMDecoder(Provider
Key/Certificate factory provider withFactory, PBEKeySpec PBEKeySpec for EncryptedPrivateKeyInfo decryption withKeySpec)Creates an instance with a specific provider and/or password. |
| Modifier and Type | Method and Description |
|---|---|
| private BinaryEncodable | |
| public BinaryEncodable | |
| public BinaryEncodable | Returns: aBinaryEncodableInputStream containing PEM dataDecodes and returns a |
| public < class type parameter that extends S extends BinaryEncodable> SBinaryEncodable | |
| public < class type parameter that extends S extends BinaryEncodable> SBinaryEncodable | Returns: aBinaryEncodable of type tClassan is, Class<S> InputStream containing PEM datathe returned object class that extends or implements
tClass)BinaryEncodableDecodes and returns a |
| private CertificateFactory | |
| private KeyFactory | |
| public static PEMDecoder | |
| public PEMDecoder | Returns: a newPEMDecoder instance configured for decryptionthe password to decrypt the encrypted PEM data. This array
is cloned and stored in the new instance. password)Returns a copy of this |
| public PEMDecoder | Returns: a newPEMDecoder instance configured with the Providerthe factory provider)ProviderReturns a copy of this |
| factory | back to summary |
|---|---|
| private final Provider factory | |
| keySpec | back to summary |
|---|---|
| private final PBEKeySpec keySpec | |
| PEM_DECODER | back to summary |
|---|---|
| private static final PEMDecoder PEM_DECODER | |
| PEMDecoder | back to summary |
|---|---|
| private PEMDecoder(Provider withFactory, PBEKeySpec withKeySpec) Creates an instance with a specific provider and/or password.
| |
| decode | back to summary |
|---|---|
| private BinaryEncodable decode(PEM pem) After the header, footer, and base64 have been separated, identify the header and footer and proceed with decoding the base64 for the appropriate type. | |
| decode | back to summary |
|---|---|
| public BinaryEncodable decode(String str) Decodes and returns a This method reads the A The input is interpreted as
| |
| decode | back to summary |
|---|---|
| public BinaryEncodable decode(InputStream is) throws IOException Decodes and returns a This method reads from the A If no PEM data is found, an
| |
| decode | back to summary |
|---|---|
| public <S extends BinaryEncodable> S decode(String str, Class<S> tClass) Decodes and returns a
This method reads the If The input is interpreted as
| |
| decode | back to summary |
|---|---|
| public <S extends BinaryEncodable> S decode(InputStream is, Class<S> tClass) throws IOException Decodes and returns a
This method reads from the If If no PEM data is found, an
| |
| getCertFactory | back to summary |
|---|---|
| private CertificateFactory getCertFactory(String algorithm) | |
| getKeyFactory | back to summary |
|---|---|
| private KeyFactory getKeyFactory(String algorithm) | |
| of | back to summary |
|---|---|
| public static PEMDecoder of() Returns the default
| |
| withDecryption | back to summary |
|---|---|
| public PEMDecoder withDecryption(char[] password) Returns a copy of this
| |
| withFactoriesOf | back to summary |
|---|---|
| public PEMDecoder withFactoriesOf(Provider provider) Returns a copy of this
| |