public abstract class Pem extends Object
Services for working with PEM encoded private keys, public keys and certificates.
PublicKey readPublicKey(Path path) {
try (ReadableByteChannel pem = Files.newByteChannel(path)) {
return Pem.decoder()
.decodePublicKey(pem);
}
}
PrivateKey readPrivateKey(Path path) {
try (ReadableByteChannel pem = Files.newByteChannel(path)) {
return Pem.decoder()
.decodePrivateKey(pem);
}
}
PrivateKey readPrivateKey(Path path, char[] passphrase) {
try (ReadableByteChannel pem = Files.newByteChannel(path)) {
return Pem.decoder()
.with(passphrase)
.decodePrivateKey(pem);
}
}
void writePublicKey(Path path,final PublicKey publicKey) {
try (WritableByteChannel pem = Files.newByteChannel(path)) {
return Pem.encoder()
.write(pem,publicKey);
}
}
void writePrivateKey(Path path,final PrivateKey privateKey) {
try (WritableByteChannel pem = Files.newByteChannel(path)) {
return Pem.encoder()
.write(pem,privateKey);
}
}
void writePkcs1PrivateKey(Path path,final PrivateKey privateKey, char[] passphrase) {
try (WritableByteChannel pem = Files.newByteChannel(path)) {
return Pem.encoder()
.with(Pem.Format.LEGACY)
.with(passphrase)
.write(pem,privateKey);
}
}
| Modifier and Type | Class and Description |
|---|---|
static class |
Pem.Decoder
Decodes PEM encoded text streams into the desired format
|
static class |
Pem.Encoder
Encode certificates, public keys and private keys into PEM encoded format
|
static class |
Pem.Encryption
Configures how private keys are encrypted
|
static class |
Pem.Format
Denotes the specific syntax used for encoding public and private keys (Does not affect the
encoding of certificates)
|
static interface |
Pem.Passphrase
Holds a passphrase using a try-with-resources model to assure the passphrase plain-text is
erased once it has been used
|
| Modifier and Type | Method and Description |
|---|---|
static Pem.Decoder |
decoder()
Return a decoder for certificates, public keys and unencrypted private keys.
|
static Pem.Encoder |
encoder()
Return an encoder that does not encrypt private keys and formats private keys in PKCS8, and
public keys or certificates in X509.
|
public static Pem.Encoder encoder()
Return an encoder that does not encrypt private keys and formats private keys in PKCS8, and public keys or certificates in X509.
To encode encrypted private keys call Encoder#with(Encryption)
To format private keys and public keys in PKCS1, call Encoder#with(Format) e.g.:
Pem.Encoder encoder = Pem.encoder().with(Format.LEGACY);
public static Pem.Decoder decoder()
Return a decoder for certificates, public keys and unencrypted private keys.
To decode encrypted private keys call Decoder#with(Passphrase).
Copyright © 2016–2024. All rights reserved.