$ java --enable-preview PemDemo (JDK 27) --- public key, encoded -----BEGIN PUBLIC KEY----- ... -----END PUBLIC KEY----- --- private key, encoded -----BEGIN PRIVATE KEY----- ... -----END PRIVATE KEY----- CHECK ok : a PublicKey encodes as BEGIN PUBLIC KEY CHECK ok : a PrivateKey encodes as BEGIN PRIVATE KEY (PKCS#8) CHECK ok : the decoded public key equals the original CHECK ok : the decoded private key equals the original --- decode(String) returns whatever the header says it is -----BEGIN PUBLIC KEY----- -> PublicKey (EC) -----BEGIN PRIVATE KEY----- -> PrivateKey (EC) --- encrypted private key -----BEGIN ENCRYPTED PRIVATE KEY----- CHECK ok : withEncryption produces ENCRYPTED PRIVATE KEY decoded with no password: javax.crypto.EncryptedPrivateKeyInfo CHECK ok : without a password you get the still-encrypted structure back CHECK ok : with the password you get the original private key --- PEM(type, byte[]) does NOT Base64-encode: the bytes go in as they are -----BEGIN ANKURM DEMO----- hello, pem -----END ANKURM DEMO----- CHECK ok : raw bytes appear verbatim between the header and footer --- give it the Base64 text and it round-trips -----BEGIN ANKURM DEMO----- aGVsbG8sIHBlbQ== -----END ANKURM DEMO----- CHECK ok : type, Base64 text (content()) and decoded payload (decode()) all survive exit=0