# How much extra information is in an RSA public key?

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?

• 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? Dec 11 '12 at 18:20
• 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. Dec 12 '12 at 7:35
• Update: No need for calculation, the modulus can be accessed directly in ruby. Jan 8 '13 at 15:48

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.

• Is the part of exponent and modulus always the same amount of bytes? Dec 13 '12 at 8:47
• @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. Jan 2 '15 at 11:07

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.

• 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.
– fgrieu
Jan 2 '15 at 14:44