$ java --enable-preview -cp ... Failures (27.0.1.0.2+1) == the file holds a different kind of object than the class you asked for certificate as PublicKey !! java.lang.ClassCastException: Cannot cast sun.security.x509.X509CertImpl to java.security.PublicKey private key as PublicKey !! java.lang.ClassCastException: Cannot cast sun.security.rsa.RSAPrivateCrtKeyImpl to java.security.PublicKey RSA private key as KeyPair !! java.lang.ClassCastException: Cannot cast sun.security.rsa.RSAPrivateCrtKeyImpl to java.security.KeyPair == untyped decode: the result type depends on what the file contains RSA PKCS#8 -> PrivateKey RSA EC PKCS#8 (openssl embeds the public key) -> KeyPair EC PKCS#8, asked for PrivateKey -> PrivateKey EC EC PKCS#8, asked for KeyPair -> KeyPair == the old OpenSSL formats RSA PRIVATE KEY (PKCS#1) -> PrivateKey RSA EC PRIVATE KEY (SEC1), untyped -> PEM(type=EC PRIVATE KEY) - NOT decoded EC PRIVATE KEY (SEC1), asked for PrivateKey !! java.lang.ClassCastException: Cannot cast java.security.PEM to java.security.PrivateKey as a PEM object: type=EC PRIVATE KEY, decoded bytes=121 == malformed text empty string !! java.lang.IllegalArgumentException: java.io.EOFException: No data available no PEM in the text !! java.lang.IllegalArgumentException: java.io.EOFException: No PEM data found footer missing !! java.lang.IllegalArgumentException: java.io.EOFException: Incomplete header body is not Base64 of a key !! java.lang.IllegalArgumentException: java.io.IOException: No recognized algorithm detected in encoding Windows line endings (CRLF) -> PublicKey RSA comment lines before the header -> PublicKey RSA == several objects in one string: only the first is returned decode(String) on a 2-certificate bundle -> CN=localhost exit=0