APIdia Launch Partner
Top Description Fields Constructors Methods
java.security

public final Class PEMDecoder

extends Object
Class Inheritance
Annotations
@PreviewFeature
feature:PEM_API
Imports
jdk.internal.javac.PreviewFeature, jdk.internal.ref.CleanerFactory, sun.security.pkcs.PKCS8Key, sun.security.rsa.RSAPrivateCrtKeyImpl, sun.security.util.KeyUtil, .Pem, javax.crypto.EncryptedPrivateKeyInfo, .CryptoException, javax.crypto.spec.PBEKeySpec, java.io.*, java.lang.ref.Reference, java.nio.charset.StandardCharsets, java.security.cert.*, java.security.spec.*, java.util.Objects

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:

When used with a PEMDecoder instance configured for decryption:

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:

When used with a PEMDecoder instance configured for decryption:

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

Since
25
External Specification
https://www.rfc-editor.org/info/rfc1421, https://www.rfc-editor.org/info/rfc5958, https://www.rfc-editor.org/info/rfc7468
See Also
PEMEncoder, PEM, EncryptedPrivateKeyInfo, BinaryEncodable

Field Summary

Modifier and TypeField and Description
private final Provider
private final PBEKeySpec
private static final PEMDecoder

Constructor Summary

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

Method Summary

Modifier and TypeMethod and Description
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.

public BinaryEncodable

Returns:

a BinaryEncodable
decode
(String
a String containing PEM data
str
)

Decodes and returns a BinaryEncodable from the given String.

public BinaryEncodable

Returns:

a BinaryEncodable
decode
(InputStream
InputStream containing PEM data
is
)

Decodes and returns a BinaryEncodable from the given InputStream.

public <
class type parameter that extends BinaryEncodable
S extends BinaryEncodable
>
S

Returns:

a BinaryEncodable specified by tClass
decode
(String
the String containing PEM data
str
,
Class<S>
the returned object class that extends or implements BinaryEncodable
tClass
)

Decodes and returns a BinaryEncodable of the specified class from the given PEM string.

public <
class type parameter that extends BinaryEncodable
S extends BinaryEncodable
>
S

Returns:

a BinaryEncodable of type tClass
decode
(InputStream
an InputStream containing PEM data
is
,
Class<S>
the returned object class that extends or implements BinaryEncodable
tClass
)

Decodes and returns a BinaryEncodable of the specified class from the given InputStream.

private CertificateFactory
private KeyFactory
getKeyFactory(String algorithm)

public static PEMDecoder

Returns:

the default PEMDecoder
of
()

Returns the default PEMDecoder instance.

public PEMDecoder

Returns:

a new PEMDecoder instance configured for decryption
withDecryption
(char[]
the password to decrypt the encrypted PEM data. This array is cloned and stored in the new instance.
password
)

Returns a copy of this PEMDecoder that decodes and decrypts encrypted private keys using the specified password.

public PEMDecoder

Returns:

a new PEMDecoder instance configured with the Provider
withFactoriesOf
(Provider
the factory Provider
provider
)

Returns a copy of this PEMDecoder instance that uses KeyFactory and CertificateFactory implementations from the specified Provider to produce cryptographic objects.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

factoryback to summary
private final Provider factory
keySpecback to summary
private final PBEKeySpec keySpec
PEM_DECODERback to summary
private static final PEMDecoder PEM_DECODER

Constructor Detail

PEMDecoderback to summary
private PEMDecoder(Provider withFactory, PBEKeySpec withKeySpec)

Creates an instance with a specific provider and/or password.

Parameters
withFactory:Provider

Key/Certificate factory provider

withKeySpec:PBEKeySpec

PBEKeySpec for EncryptedPrivateKeyInfo decryption

Method Detail

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

decodeback to summary
public BinaryEncodable decode(String str)

Decodes and returns a BinaryEncodable from the given String.

This method reads the String until PEM data is found or the end of the String is reached. If no PEM data is found, an IllegalArgumentException is thrown.

A BinaryEncodable is returned that best represents the decoded content. If the PEM type is not supported, a PEM object is returned containing the type identifier, Base64-encoded data, and any leading data preceding the PEM header. For BinaryEncodable types other than PEM, leading data is ignored.

The input is interpreted as UTF-8.

Parameters
str:String

a String containing PEM data

Returns:BinaryEncodable

a BinaryEncodable

Exceptions
IllegalArgumentException:
if decoding fails or no PEM data is found
NullPointerException:
if str is null
CryptoException:
if an error occurs during decryption
Since
27
decodeback to summary
public BinaryEncodable decode(InputStream is) throws IOException

Decodes and returns a BinaryEncodable from the given InputStream.

This method reads from the InputStream until the end of a PEM footer or the end of the stream. If an I/O error occurs, the stream position may become inconsistent. Further decoding operations on the same InputStream are not recommended.

A BinaryEncodable is returned that best represents the decoded content. If the PEM type is not supported, a PEM object is returned containing the type identifier, Base64-encoded data, and any leading data preceding the PEM header. For BinaryEncodable types other than PEM, leading data is ignored.

If no PEM data is found, an EOFException is thrown.

Parameters
is:InputStream

InputStream containing PEM data

Returns:BinaryEncodable

a BinaryEncodable

Exceptions
IOException:
if an I/O error occurs or PEM syntax is invalid
EOFException:
if no PEM data is found or the stream ends unexpectedly
IllegalArgumentException:
if decoding fails
NullPointerException:
if InputStream is null
CryptoException:
if an error occurs during decryption
Since
27
decodeback to summary
public <S extends BinaryEncodable> S decode(String str, Class<S> tClass)

Decodes and returns a BinaryEncodable of the specified class from the given PEM string.

tClass must be an appropriate class for the PEM type.

This method reads the String until PEM data is found or the end of the String is reached. If no PEM data is found, an IllegalArgumentException is thrown.

If tClass is PEM.class, a PEM object is returned containing the type identifier, Base64-encoded data, and any leading data preceding the PEM header. For BinaryEncodable types other than PEM, leading data is ignored.

The input is interpreted as UTF-8.

Parameters
<S>
class type parameter that extends BinaryEncodable
str:String

the String containing PEM data

tClass:Class<S>

the returned object class that extends or implements BinaryEncodable

Returns:S

a BinaryEncodable specified by tClass

Exceptions
IllegalArgumentException:
on error in decoding or no PEM data found
ClassCastException:
if tClass does not represent the PEM type
NullPointerException:
if any input values are null
CryptoException:
if an error occurs during decryption
Since
27
decodeback to summary
public <S extends BinaryEncodable> S decode(InputStream is, Class<S> tClass) throws IOException

Decodes and returns a BinaryEncodable of the specified class from the given InputStream.

tClass must be an appropriate class for the PEM type.

This method reads from the InputStream until the end of a PEM footer or the end of the stream. If an I/O error occurs, the stream position may become inconsistent. Further decoding operations on the same InputStream are not recommended.

If tClass is PEM.class, a PEM object is returned containing the type identifier, Base64-encoded data, and any leading data preceding the PEM header. For BinaryEncodable types other than PEM, leading data is ignored.

If no PEM data is found, an EOFException is thrown.

Parameters
<S>
class type parameter that extends BinaryEncodable
is:InputStream

an InputStream containing PEM data

tClass:Class<S>

the returned object class that extends or implements BinaryEncodable

Returns:S

a BinaryEncodable of type tClass

Exceptions
IOException:
if an I/O error occurs or PEM syntax is invalid
EOFException:
if no PEM data is found or the stream ends unexpectedly
IllegalArgumentException:
if decoding fails
ClassCastException:
if tClass does not represent the PEM type
NullPointerException:
if any input values are null
CryptoException:
if an error occurs during decryption
Since
27
See Also
decode(InputStream), decode(String, Class)
getCertFactoryback to summary
private CertificateFactory getCertFactory(String algorithm)
getKeyFactoryback to summary
private KeyFactory getKeyFactory(String algorithm)
ofback to summary
public static PEMDecoder of()

Returns the default PEMDecoder instance.

Returns:PEMDecoder

the default PEMDecoder

withDecryptionback to summary
public PEMDecoder withDecryption(char[] password)

Returns a copy of this PEMDecoder that decodes and decrypts encrypted private keys using the specified password. Unencrypted PEM can also be decoded by the returned instance.

Parameters
password:char[]

the password to decrypt the encrypted PEM data. This array is cloned and stored in the new instance.

Returns:PEMDecoder

a new PEMDecoder instance configured for decryption

Exceptions
NullPointerException:
if password is null
withFactoriesOfback to summary
public PEMDecoder withFactoriesOf(Provider provider)

Returns a copy of this PEMDecoder instance that uses KeyFactory and CertificateFactory implementations from the specified Provider to produce cryptographic objects. Any errors using the Provider will occur during decoding.

Parameters
provider:Provider

the factory Provider

Returns:PEMDecoder

a new PEMDecoder instance configured with the Provider

Exceptions
NullPointerException:
if provider is null
Since
27