Class PEMEncoder
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 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 BinaryEncodablePREVIEW. 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 PEMPREVIEW object, the API surrounds
PEM.content()PREVIEW with a PEM header and footer based on
PEM.type()PREVIEW. The value returned by PEM.leadingData()PREVIEW 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 KEYPEMPREVIEW:PEM.type()PREVIEW
When used with a PEMEncoder instance configured for encryption:
PrivateKey: ENCRYPTED PRIVATE KEYKeyPair: ENCRYPTED PRIVATE KEYPKCS8EncodedKeySpec: ENCRYPTED PRIVATE KEY
This 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.
- Since:
- 25
- External Specifications
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbyte[]Encodes the specifiedBinaryEncodableand returns a PEM-encoded byte array.Encodes the specifiedBinaryEncodableand returns a PEM-encoded string.static PEMEncoderPREVIEWof()Returns an instance ofPEMEncoder.withEncryption(char[] password) Returns a copy of thisPEMEncoderconfigured to encrypt and encode using the specified password and the default encryption algorithm.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Method Details
-
of
-
encodeToString
Encodes the specifiedBinaryEncodableand returns a PEM-encoded string.- Parameters:
be- theBinaryEncodableto encode- Returns:
- a
Stringcontaining the PEM-encoded data - Throws:
IllegalArgumentException- ifbehas no encoding, is an unsupported class, or cannot be used with encryptionNullPointerException- ifbeisnullCryptoExceptionPREVIEW- if an error occurs during encryption- Since:
- 27
- See Also:
-
encode
Encodes the specifiedBinaryEncodableand returns a PEM-encoded byte array.- Parameters:
be- theBinaryEncodableto encode- Returns:
- a PEM-encoded byte array
- Throws:
IllegalArgumentException- ifbehas no encoding, is an unsupported class, or cannot be used with encryptionNullPointerException- ifbeisnullCryptoExceptionPREVIEW- if an error occurs during encryption- Since:
- 27
- See Also:
-
withEncryption
Returns a copy of thisPEMEncoderconfigured to encrypt and encode using the specified password and the default encryption algorithm.Only
PrivateKey,KeyPair, andPKCS8EncodedKeySpecobjects can be encoded with this newly configured instance. Attempting to encode otherBinaryEncodableobjects will throw anIllegalArgumentException.To use non-default encryption parameters or a different provider, use an
encryptmethod inEncryptedPrivateKeyInfo, then pass the resulting object toencode(BinaryEncodable).- Implementation Note:
- The
jdk.epkcs8.defaultAlgorithmsecurity property defines the default encryption algorithm. TheAlgorithmParameterSpecdefaults are determined by the provider. - Parameters:
password- the encryption password. The array is cloned and stored in the new instance.- Returns:
- a new
PEMEncoderinstance configured for encryption - Throws:
NullPointerException- ifpasswordisnullCryptoExceptionPREVIEW- if generating the encryption key fails
-
PEMEncoderwhen preview features are enabled.