101

Using OpenSSL from the command line in Linux, is there some way to examine a key (either public or private) to determine the key size?

jdw
  • 3,855
  • 2
  • 17
  • 21
  • 1
    There are many different ways depending on the format of the keypair. Were you looking at a specific format? – Zoredache Oct 27 '11 at 19:55
  • 1
    As a rule of thumb, the size (in bytes) of a .pem RSA private key is roughly 3/4 of the size of the key length (in bits) - e.g. a 4096-bit key might be roughly 3247 bytes. File sizes do vary though. – mwfearnley Dec 03 '19 at 11:34

2 Answers2

127
openssl rsa -in private.key -text -noout

The top line of the output will display the key size.

For example:

Private-Key: (2048 bit)

To view the key size from a certificate:

openssl x509 -in public.pem -text -noout | grep "Public Key"
RSA Public Key: (2048 bit)
Paul
  • 3,037
  • 6
  • 27
  • 40
Shane Madden
  • 114,520
  • 13
  • 181
  • 251
21

The first (2048) is the bit length of the key:

 $ ssh-keygen -lf /etc/ssh/rsa_key.pub 
 2048 91:1c:ae:17:16:...
Adrien P.
  • 789
  • 3
  • 9