# How to determine the RSA Private key size from the Public.key file?

Question: How to determine the RSA Private key size from the Public.key file? Just roughly, how big it could be?

$ls -la Public.key -rw-r--r--. 1 user user 498 Sep 4 15:31 Public.key$


The Public.key was generated using the Java API (which defaults to the X509 SubjectPublicKeyInfo structure with embedded PKCS#1 public key in a BIT STRING).

• How was the file created? OpenSSL, OpenSSH, something else? Sep 11, 2015 at 15:45
• The private key is usually of roughly the same size as the public modulus so extracting the length of the modulus may suffice. Sep 11, 2015 at 17:19
• It was created in a JAVA application Sep 11, 2015 at 17:19
• Do you know what functions were called to create the key pair? Any preprocessing done before saving the file? How do you know that in the code they don't just write the public key file and then choose a random number between 100 and 500 and write that many random bytes to the file also? Sep 11, 2015 at 17:41
• By private key size do you mean the modulus size, the private exponent size, the size of a private key file, or something else?
– otus
Sep 12, 2015 at 11:56

Assumption: you've saved the RSA public key using the default encoding in Java (for the SUNRSA provider), as in:

KeyPairGenerator kgen = KeyPairGenerator.getInstance("RSA");
kgen.initialize(1024);
KeyPair kp = kgen.generateKeyPair();
Files.write(new File("Public.key").toPath(), kp.getPublic().getEncoded());


This generates an X.509 ASN.1 SubjectPublicKeyInfo structure encoded using DER.

In that case you can retrieve the information from the public key using the following OpenSSL command:

openssl rsa -pubin -inform DER -in Public.key -text -noout


This will show the following output:

Public-Key: (1024 bit)
Modulus:
00:c3:7e:ea:ba:9d:55:81:ec:80:8b:68:fb:7b:80:
14:d4:66:45:0e:ba:6e:45:98:23:dc:24:26:a4:30:
0e:20:48:12:23:b3:8b:5e:ed:c2:a5:cf:5e:32:43:
00:9b:61:6e:e7:a6:f4:01:55:e9:cc:a8:c7:29:24:
fd:29:7b:a1:b9:4b:1c:4c:de:cc:f6:d0:7e:75:a6:

• openssl asn1parse of SPKI shows you one BITSTRING for the whole PKCS#1; to see them broken-out (including modulus) do another asn1parse with -strparse specifying the offset of the BITSTRING. Alternatively openssl rsa -inform d -pubin -text or (version 1.0.0 and up) openssl pkey -inform d -pubin -text displays everything including bitsize in one step. Sep 12, 2015 at 12:02
• @dave_thompson_085 Dangit, I really need to do my development again from the VM. Working on Windows only is severely limiting my capabilities. I'll try and verify your openssl commands asap and integrate them in my answer. Sep 12, 2015 at 12:44