Inside a shell script I want verify a public RSA file. I want to find a way to check if this file is a genuine public key file, nothing else.

What are the ways I can verify this input file to check this is a genuine public key file, not a regular file?

I will be using this public key file in the future to validate an incoming encrypt gzip file, but that is out of scope for now.

I want to validate the input file to check its genuine RSA public key file is not an ordinary file. Please note that I do not have any other files with me (for example, a private key).

For example, if the file is ‘public.pem’ I just want check inside that it’s a genuine RSA public key file, not just a file with texts or file is not corrupted. I’m already checking that file is not zero sized and the MD5 hash.

Other possible checks I found

Check the file contains the text ‘BEGIN PUBLIC KEY’ and ‘END PUBLIC KEY’. I also found the following command using Google Search. Is there a better way to do this using OpenSSL?

openssl rsa -noout -text -inform PEM -in pubkey.pem -pubin

1 Answer 1


I found a satisfying answer in an answer to Stack Overflow question How to check a public RSA key file.

Not the answer you're looking for? Browse other questions tagged or ask your own question.