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:
X509Certificate: CERTIFICATEX509CRL: X509 CRLPublicKey: PUBLIC KEYPrivateKey: PRIVATE KEYEncryptedPrivateKeyInfo: ENCRYPTED PRIVATE KEYKeyPair: PRIVATE KEYX509EncodedKeySpec: PUBLIC KEYPKCS8EncodedKeySpec: PRIVATE KEYPEM: PEM#type() When used with a PEMEncoder instance configured for encryption:
PrivateKey: ENCRYPTED PRIVATE KEYKeyPair: ENCRYPTED PRIVATE KEYPKCS8EncodedKeySpec: ENCRYPTED PRIVATE KEYThis class is immutable and thread-safe.
Example: encode a private key:
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);
Implementation Note
Implementations may support additional PEM types.
PEMDecoder, PEM, EncryptedPrivateKeyInfo
| Modifier and Type | Field and Description |
|---|---|
| private final SecretKey | |
| private static final PEMEncoder |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private byte[] | buildKey(byte[] publicEncoding, byte[] privateEncoding)
Build the PEM encoding for AsymmetricKey and KeyPair |
| public byte[] | Returns: a PEM-encoded byte arraythe be)BinaryEncodable to encodeEncodes the specified |
| public String | Returns: aString containing the PEM-encoded datathe be)BinaryEncodable to encodeEncodes the specified |
| public static PEMEncoder | |
| public PEMEncoder | Returns: a newPEMEncoder instance configured for encryptionthe encryption password. The array is cloned and
stored in the new instance. password)Returns a copy of this |
| key | back to summary |
|---|---|
| private final SecretKey key | |
| PEM_ENCODER | back to summary |
|---|---|
| private static final PEMEncoder PEM_ENCODER | |
| PEMEncoder | back to summary |
|---|---|
| private PEMEncoder(PBEKeySpec keySpec) Creates a PEMEncoder instance configured for the given keySpec. | |
| buildKey | back to summary |
|---|---|
| private byte[] buildKey(byte[] publicEncoding, byte[] privateEncoding) Build the PEM encoding for AsymmetricKey and KeyPair | |
| encode | back to summary |
|---|---|
| public byte[] encode(BinaryEncodable be) Encodes the specified
| |
| encodeToString | back to summary |
|---|---|
| public String encodeToString(BinaryEncodable be) Encodes the specified
| |
| of | back to summary |
|---|---|
| public static PEMEncoder of() Returns an instance of
| |
| withEncryption | back to summary |
|---|---|
| public PEMEncoder withEncryption(char[] password) Returns a copy of this Only To use non-default encryption parameters or a different provider, use
an Implementation Note The
| |