How to extract public key using OpenSSL?

131

The following command generates a file which contains both public and private key:

openssl genrsa -des3 -out privkey.pem 2048

Source: here

With OpenSSL, the private key contains the public key information as well, so a public key doesn't need to be generated separately

How can we extract the public key from the privkey.pem file?

Thanks.

This question is tagged with openssl public-key-encryption pki

~ Asked on 2012-04-22 19:12:57

The Best Answer is


199

openssl rsa -in privkey.pem -pubout > key.pub

That writes the public key to key.pub

~ Answered on 2012-04-22 19:19:02


144

Though, the above technique works for the general case, it didn't work on Amazon Web Services (AWS) PEM files.

I did find in the AWS docs the following command works: ssh-keygen -y

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html

edit Thanks @makenova for the complete line:

ssh-keygen -y -f key.pem > key.pub

~ Answered on 2014-05-01 17:36:29


Most Viewed Questions: