java.lng.Exception: `Input not an X.509 certificate` when importing .pem certificate

I have created a self-signed certificate with makecert, exported it with private key to .pfx file and imported on the server. Then I copied one on the client and tried importing it using keytool. Got an error `Input is not an X.509 certificate`.
So I converted .pfx  certificate to .pem using openssl and tried again - same result.

I did some research and found that I might need to convert it to .der, but it still might not work. Apparently keytool only supports single certificate PEM files. Even though mine is a single certificate, PEM file contains private key information:
    -----BEGIN PRIVATE KEY----
    -----END PRIVATE KEY------

    ----END CERTIFICATE-------

Open in new window

So I am not sure what should be my next step to ensure import will work when done with keytool on the client.
Can anyone shed some light on this issue?
LVL 35
Who is Participating?
YZlatConnect With a Mentor Author Commented:
The solution was much simpler - I opened the file in text editor and deleted everything except the lines in between

    ----END CERTIFICATE-------

the client machine should not have a private key. So after removing private key and other lines it worked with keytool
btanExec ConsultantCommented:
I suggest either you try the online conversion for the sake of testing since it is development key or use openssl
Convert a DER file (.crt .cer .der) to PEM openssl x509 -inform der -in certificate.cer -out certificate.pem

•Convert a PEM file to DER openssl x509 -outform der -in certificate.pem -out certificate.der

•Convert a PKCS#12 file (.pfx .p12) containing a private key and certificates to PEM openssl pkcs12 -in keyStore.pfx -out keyStore.pem -nodes

You can add -nocerts to only output the private key or add -nokeys to only output the certificates.
btanExec ConsultantCommented:
For clarity of PEM and DER which the latter is used for Java platform which is what you are looking at.
The PEM format is usually having extensions such as .pem, .crt, .cer, and .key. They are Base64 encoded ASCII files and contain "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" statements. Server certificates, intermediate certificates, and private keys can all be put into the PEM format.

The DER format is simply a binary form of a certificate instead of the ASCII PEM format. It sometimes has a file extension of .der but it often has a file extension of .cer so the only way to tell the difference between a DER .cer file and a PEM .cer file is to open it in a text editor and look for the BEGIN/END statements. All types of certificates and private keys can be encoded in DER format. DER is typically used with Java platforms.
YZlatAuthor Commented:
solved it myself
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.