1
$\begingroup$

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

$\endgroup$
5
  • $\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

2
$\begingroup$

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:
    5f:28:09:65:67:76:1c:e8:f5:ae:85:ad:4a:dc:48:
    ca:82:46:25:e1:17:f8:3b:0c:53:30:c4:bb:08:5a:
    75:43:8a:d3:26:97:35:a7:2f:61:c6:68:06:24:b3:
    c8:42:e1:53:40:39:95:46:c9
Exponent: 65537 (0x10001)

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

$\endgroup$
3
  • 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.