Unable to verify a public cert's modules with openssl (Unable to verify rsa public key came from a private key)

I created a private and public key pair like this:

openssl genrsa -out priv.pem 2048
openssl rsa -in priv.pem -pubout -out pub.pem

I want to now verify the public key came from the private key though openssl. I am able to hash the modulus like this:

openssl rsa -noout -modulus -in priv.pem | openssl md5

However when I try to hash the modulus of the public key I get an error:

penssl x509 -noout -modulus -in pub.pem | openssl md5
unable to load certificate
140020691816896:error:0909006C:PEM routines:get_name:no start line:../crypto/pem/pem_lib.c:745:Expecting: TRUSTED CERTIFICATE
(stdin)= d41d8cd98f00b204e9800998ecf8427e

I know I am going about this wrong, however I do not know of a way to actually test if a public key came from a private key correctly. How can I accomplish this in inux with openssl only based on the way I created the key pair asside from encrypting a file with my public key and then trying to decrypt it with the private?


You can use the same command you used for the private key, but using the -pubin argument. For example:

openssl rsa -noout -modulus -in pub.pem -pubin | openssl md5


The x509 command expects an X.509 certificate as input. You haven't created a certificate, and are providing only a public key as input. A certificate is more than a public key; it is a binding between identity information and a public key.