Search code examples
cryptographyrsapycryptodome

why is rsa key size_in_bytes calculated this way


I'm trying to understand the code that returns the size_in_bytes for a RSA key (I'm looking at PyCryptoDomex). They calculate it:

def size_in_bytes(self):
    return (self.size_in_bits() - 1) // 8 + 1

I would have thought that it would simply be

self.size_in_bits()//8

The specific code is here: github_code_location

I'm sure there's a reason to subtract 1 from the bits and then add 1 to the integer after division but I'd like to understand why.


Solution

  • 7 // 8 == 0
    

    But you cannot store 7 bits in zero bytes.