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