APIdia Launch Partner
Top Description Fields Constructors Methods
java.security

public final Class PEMEncoder

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

Preview

Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Programs can only use PEMEncoder when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

PEMEncoder implements an encoder 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 a Base64-encoded content enclosed by a type-identifying header and footer.

Encoding can be performed on cryptographic objects that implement BinaryEncodable. The encode(BinaryEncodable) and encodeToString(BinaryEncodable) methods encode a BinaryEncodable into PEM and return the data in a byte array or String.

Private keys can be encrypted and encoded by configuring a PEMEncoder with the withEncryption(char[]) method, which takes a password and returns a new PEMEncoder instance configured to encrypt the key with that password.

PKCS #8 v2.0 defines the ASN.1 OneAsymmetricKey structure, which may contain both private and public keys. KeyPair objects passed to the encode or encodeToString methods are encoded as a OneAsymmetricKey structure using the "PRIVATE KEY" type.

When encoding a PEM object, the API surrounds PEM#content() with a PEM header and footer based on PEM#type(). The value returned by PEM#leadingData() is not included in the output.

The following lists the supported BinaryEncodable classes and the PEM types they encode to:

When used with a PEMEncoder instance configured for encryption:

This class is immutable and thread-safe.

Example: encode a private key:

PEMEncoder pe = PEMEncoder.of(); byte[] pemData = pe.encode(privKey);
PEMEncoder pe = PEMEncoder.of();
byte[] pemData = pe.encode(privKey);

Example: encrypt and encode a private key using a password:

PEMEncoder pe = PEMEncoder.of().withEncryption(password); byte[] pemData = pe.encode(privKey);
PEMEncoder pe = PEMEncoder.of().withEncryption(password);
byte[] pemData = pe.encode(privKey);

Implementation Note

Implementations may support 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
PEMDecoder, PEM, EncryptedPrivateKeyInfo

Field Summary

Modifier and TypeField and Description
private final SecretKey
private static final PEMEncoder

Constructor Summary

AccessConstructor and Description
private
PEMEncoder(PBEKeySpec keySpec)

Creates a PEMEncoder instance configured for the given keySpec.

Method Summary

Modifier and TypeMethod and Description
private byte[]
buildKey(byte[] publicEncoding, byte[] privateEncoding)

Build the PEM encoding for AsymmetricKey and KeyPair

public byte[]

Returns:

a PEM-encoded byte array
encode
(BinaryEncodable
the BinaryEncodable to encode
be
)

Encodes the specified BinaryEncodable and returns a PEM-encoded byte array.

public String

Returns:

a String containing the PEM-encoded data
encodeToString
(BinaryEncodable
the BinaryEncodable to encode
be
)

Encodes the specified BinaryEncodable and returns a PEM-encoded string.

public static PEMEncoder

Returns:

a PEMEncoder
of
()

Returns an instance of PEMEncoder.

public PEMEncoder

Returns:

a new PEMEncoder instance configured for encryption
withEncryption
(char[]
the encryption password. The array is cloned and stored in the new instance.
password
)

Returns a copy of this PEMEncoder configured to encrypt and encode using the specified password and the default encryption algorithm.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

keyback to summary
private final SecretKey key
PEM_ENCODERback to summary
private static final PEMEncoder PEM_ENCODER

Constructor Detail

PEMEncoderback to summary
private PEMEncoder(PBEKeySpec keySpec)

Creates a PEMEncoder instance configured for the given keySpec.

Method Detail

buildKeyback to summary
private byte[] buildKey(byte[] publicEncoding, byte[] privateEncoding)

Build the PEM encoding for AsymmetricKey and KeyPair

encodeback to summary
public byte[] encode(BinaryEncodable be)

Encodes the specified BinaryEncodable and returns a PEM-encoded byte array.

Parameters
be:BinaryEncodable

the BinaryEncodable to encode

Returns:byte[]

a PEM-encoded byte array

Exceptions
IllegalArgumentException:
if be has no encoding, is an unsupported class, or cannot be used with encryption
NullPointerException:
if be is null
CryptoException:
if an error occurs during encryption
Since
27
See Also
withEncryption(char[])
encodeToStringback to summary
public String encodeToString(BinaryEncodable be)

Encodes the specified BinaryEncodable and returns a PEM-encoded string.

Parameters
be:BinaryEncodable

the BinaryEncodable to encode

Returns:String

a String containing the PEM-encoded data

Exceptions
IllegalArgumentException:
if be has no encoding, is an unsupported class, or cannot be used with encryption
NullPointerException:
if be is null
CryptoException:
if an error occurs during encryption
Since
27
See Also
withEncryption(char[])
ofback to summary
public static PEMEncoder of()

Returns an instance of PEMEncoder.

Returns:PEMEncoder

a PEMEncoder

withEncryptionback to summary
public PEMEncoder withEncryption(char[] password)

Returns a copy of this PEMEncoder configured to encrypt and encode using the specified password and the default encryption algorithm.

Only PrivateKey, KeyPair, and PKCS8EncodedKeySpec objects can be encoded with this newly configured instance. Attempting to encode other BinaryEncodable objects will throw an IllegalArgumentException.

To use non-default encryption parameters or a different provider, use an encrypt method in EncryptedPrivateKeyInfo, then pass the resulting object to encode(BinaryEncodable).

Implementation Note

The jdk.epkcs8.defaultAlgorithm security property defines the default encryption algorithm. The AlgorithmParameterSpec defaults are determined by the provider.

Parameters
password:char[]

the encryption password. The array is cloned and stored in the new instance.

Returns:PEMEncoder

a new PEMEncoder instance configured for encryption

Exceptions
NullPointerException:
if password is null
CryptoException:
if generating the encryption key fails