Sign up ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

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

share|improve this question
How was the file created? OpenSSL, OpenSSH, something else? – mikeazo Sep 11 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. – SEJPM Sep 11 at 17:19
It was created in a JAVA application – LoukiosValentine79 Sep 11 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? – mikeazo Sep 11 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 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.

share|improve this answer
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. – dave_thompson_085 Sep 12 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. – Maarten Bodewes Sep 12 at 12:44
FYI 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. – dave_thompson_085 Sep 14 at 2:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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