Take the 2-minute tour ×
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.

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?

share|improve this question

migrated from security.stackexchange.com Dec 11 '12 at 9:28

This question came from our site for Information security professionals.

    
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? –  Paŭlo Ebermann 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. –  user857990 Dec 12 '12 at 7:35
    
Update: No need for calculation, the modulus can be accessed directly in ruby. –  user857990 Jan 8 '13 at 15:48

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Is the part of exponent and modulus always the same amount of bytes? –  user857990 Dec 13 '12 at 8:47

Your Answer

 
discard

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.