4
$\begingroup$

I'm trying to calculate the size of an RSA public key in Ruby. I've retrieved the key in PEM format, and once I've decoded the base64 part from the PEM format, I get the size in bytes. What I find is that what I retrieve and the actual key size is off by 12 bytes, or 96 bits for that matter – for a 1024 bit key, I get 140 bytes (=1120 bits).

I know there is at least always an exponent and the modulus in the public key. My question is:

Is it always a fixed difference (12 bytes), or can the difference vary and if yes - how do I know how big the difference is?

$\endgroup$
3
  • $\begingroup$ Welcome to Cryptography Stack Exchange. Just to make sure: You are talking about an RSA key, are you? (Public keys for other schemes can look quite different). What was your key size in your example? $\endgroup$ Dec 11, 2012 at 18:20
  • $\begingroup$ Thanks! Yes, it's an RSA key. The key size I've determined is 140 bytes (1120 bits) and the actual key is 1024 bits. Hence the difference from the question. $\endgroup$
    – user857990
    Dec 12, 2012 at 7:35
  • $\begingroup$ Update: No need for calculation, the modulus can be accessed directly in ruby. $\endgroup$
    – user857990
    Jan 8, 2013 at 15:48

2 Answers 2

5
$\begingroup$

Firstly the length of an RSA key is the length of the modulus and an RSA public key consists of an exponent and a modulus, so there is a couple of bytes there.

Secondly depending on how you created and/or transmit the key it may be wrapped in a SubjectPublicKeyInfo block. It's defined as (RFC 3280):

 SubjectPublicKeyInfo ::= SEQUENCE {
   algorithm AlgorithmIdentifier,
   subjectPublicKey BIT STRING }

 AlgorithmIdentifier ::= SEQUENCE {
   algorithm OBJECT IDENTIFIER,
   parameters ANY DEFINED BY algorithm OPTIONAL }

and is basically a way of telling a receiver what algorithm the public key uses and any additional info that may be of use (probably nothing for RSA keys). So there is possibly a few extra bytes there.

If you're interested in this stuff you can look into ASN1 definitions which describe the structure in which keys and certificates (and many other things) should be passed around. If you want a quick look you can use:

openssl asn1parse -in key.prv -inform pem 

to see what is in the file but in short, depending on the encoding I wouldn't make assumptions on the data size.

$\endgroup$
2
  • $\begingroup$ Is the part of exponent and modulus always the same amount of bytes? $\endgroup$
    – user857990
    Dec 13, 2012 at 8:47
  • $\begingroup$ @user857990 No, not always, often the exponent part is shorter (3 bytes, 0x010001 is a common choice). But parsing the public key ASN1 will tell for sure. This has length fields for its elements. $\endgroup$ Jan 2, 2015 at 11:07
0
$\begingroup$

Something that might bite is that the ASN encoding requires the numbers to be positive. So when a 2048-bit random prime happens to have a '1' as the first bit, it is prefixed with a byte of 0 to make it positive. This means half of your 2048-bit primes are 256 byte and the others are 257 bytes.

$\endgroup$
1
  • 2
    $\begingroup$ Uh, no. All 2048-bit primes have the same size in ASN.1: '02' for int, '82 01 01' for the 257-byte size, '00' for sign, followed by 256 bytes with the value, starting with a byte in range '80..FF'. Also the question is about public keys, which contain a large composite $n$, and $e$ which often is prime but needs not be. $\endgroup$
    – fgrieu
    Jan 2, 2015 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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