public final class PEMEncoder extends Object
PEMEncoder is a preview API of the Java platform. PEMEncoder implements an encoder for Privacy-Enhanced Mail (PEM) data. PEM is a textual encoding used to store and transfer security objects, such as asymmetric keys, certificates, and certificate revocation lists (CRL). It is defined in RFC 1421 and RFC 7468. PEM consists of a Base64-formatted binary encoding enclosed by a type-identifying header and footer. Encoding may be performed on Java API cryptographic objects that implement DEREncodablePREVIEW. The encode(DEREncodable) and encodeToString(DEREncodable) methods encode a DEREncodable 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. Alternatively, a private key encrypted as an EncryptedKeyInfo object can be encoded directly to PEM by passing it to the encode or encodeToString methods.
PKCS #8 2.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 PEMRecordPREVIEW, the API surrounds the PEMRecord.content()PREVIEW with the PEM header and footer from PEMRecord.type()PREVIEW. PEMRecord.leadingData()PREVIEW is not included in the encoding. PEMRecord will not perform validity checks on the data.
The following lists the supported DEREncodable classes and the PEM types that each are encoded as:
X509Certificate : CERTIFICATEX509CRL : X509 CRLPublicKey: PUBLIC KEYPrivateKey : PRIVATE KEYPrivateKey (if configured with encryption): ENCRYPTED PRIVATE KEYEncryptedPrivateKeyInfo : ENCRYPTED PRIVATE KEYKeyPair : PRIVATE KEYX509EncodedKeySpec : PUBLIC KEYPKCS8EncodedKeySpec : PRIVATE KEYPEMRecord : PEMRecord.type()
This class is immutable and thread-safe.
Here is an example of encoding a PrivateKey object:
PEMEncoder pe = PEMEncoder.of();
byte[] pemData = pe.encode(privKey);
Here is an example that encrypts and encodes a private key using the specified password:
PEMEncoder pe = PEMEncoder.of().withEncryption(password);
byte[] pemData = pe.encode(privKey);
DEREncodable objects.| Modifier and Type | Method | Description |
|---|---|---|
byte[] |
encode |
Encodes the specified DEREncodable and returns the PEM encoding in a byte array. |
String |
encodeToString |
Encodes the specified DEREncodable and returns a PEM encoded string. |
static PEMEncoderPREVIEW |
of() |
Returns an instance of PEMEncoder. |
PEMEncoderPREVIEW |
withEncryption |
Returns a new PEMEncoder instance configured for encryption with the default algorithm and a given password. |
public static PEMEncoderPREVIEW of()
PEMEncoder.PEMEncoder
public String encodeToString(DEREncodablePREVIEW de)
DEREncodable and returns a PEM encoded string.de - the DEREncodable to be encodedString containing the PEM encoded dataIllegalArgumentException - if the DEREncodable cannot be encodedNullPointerException - if de is null
public byte[] encode(DEREncodablePREVIEW de)
DEREncodable and returns the PEM encoding in a byte array.de - the DEREncodable to be encodedIllegalArgumentException - if the DEREncodable cannot be encodedNullPointerException - if de is null
public PEMEncoderPREVIEW withEncryption(char[] password)
PEMEncoder instance configured for encryption with the default algorithm and a given password. Only PrivateKey objects can be encrypted with this newly configured instance. Encoding other DEREncodablePREVIEW objects will throw an IllegalArgumentException.
jdk.epkcs8.defaultAlgorithm security property and uses the default encryption parameters of the provider that is selected. For greater flexibility with encryption options and parameters, use EncryptedPrivateKeyInfo.encryptKey(PrivateKey, Key, String, AlgorithmParameterSpec, Provider, SecureRandom)PREVIEW and use the returned object with encode(DEREncodable).password - the encryption password. The array is cloned and stored in the new instance.PEMEncoder instance configured for encryptionNullPointerException - when password is null
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/security/PEMEncoder.html
PEMEncoderwhen preview features are enabled.