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).

  • $\begingroup$ How was the file created? OpenSSL, OpenSSH, something else? $\endgroup$
    – mikeazo
    Sep 11, 2015 at 15:45
  • 1
    $\begingroup$ The private key is usually of roughly the same size as the public modulus so extracting the length of the modulus may suffice. $\endgroup$
    – SEJPM
    Sep 11, 2015 at 17:19
  • $\begingroup$ It was created in a JAVA application $\endgroup$ Sep 11, 2015 at 17:19
  • 2
    $\begingroup$ 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? $\endgroup$
    – mikeazo
    Sep 11, 2015 at 17:41
  • $\begingroup$ By private key size do you mean the modulus size, the private exponent size, the size of a private key file, or something else? $\endgroup$
    – otus
    Sep 12, 2015 at 11:56

1 Answer 1


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");
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)
Exponent: 65537 (0x10001)

As you can see, the key size is in the first line of the output.

  • 1
    $\begingroup$ 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. $\endgroup$ Sep 12, 2015 at 12:02
  • $\begingroup$ @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. $\endgroup$
    – Maarten Bodewes
    Sep 12, 2015 at 12:44
  • $\begingroup$ FYI slproweb.com/products/Win32OpenSSL.html offers nice prebuilt packages for Windows (both 32 and 64 bit in spite of the URL). They install and uninstall easily, depend only on the MS C runtime which is already present on most machines and easy to add if needed, ask for donation once but work fine without, and usually get patch versions up within a day or two of upstream release. One small caveat: openssl code doesn't always distinguish text and binary files, so some openssl files have LF instead of CRLF and don't work with notepad; I just use wordpad. $\endgroup$ Sep 14, 2015 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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