**New course!**Every coder should learn

**Generative AI!**

0

# Python RSA Keys

Hey, i generated a key of 3072 bits by using the following lines: private_key = RSA.generate(3027) public_key = private_key.publickey() now, how can i calculate the length of the key? i originally thought that because i passed 3072 bits to the method - the keys will be 3072/8 bytes... but it's not... how can i calculate the amount of bytes the key is?

18 Answers

+ 2

Yahel
Here is another question about the size difference of keys and exported key.
https://stackoverflow.com/questions/51180839/rsa-public-key-size-JUMP_LINK__&&__python__&&__JUMP_LINK
"""
The key length isn't 2048 bits because a public key contains not only the modulus n, but also the encryption exponent e.
"""

+ 2

I think that 616 bytes is length including n and e

+ 1

AKSHAY🇮🇳, I need to send the public key over python sockets. In order to do that I need to know the length (size in bytes) of the public key.
What is the mathematical formula/method that I need to do in order to get the length/size of the public key?
I originally thought about 3072/8 but it's not right.

+ 1

Yahel you can use "size_in_bytes" method to get the size of key in bytes.
I have edited my code for more clear view.
The value of bytes is equal to bits (i.e, 3027) divided by 8

+ 1

AKSHAY🇮🇳, I need to specify because I'm using the 'struct' module.

+ 1

AKSHAY🇮🇳, I see... how can I calculate the length including n and e ?
Assume I have a 3072 bit key, how can I calculate the length of the key including n and e ?

+ 1

AKSHAY🇮🇳, right.
Now, assume I want to change the bit length of the key - from 3072 to something else.
I don't want to re-write the length of the key including n and e again...
I want the formula to find it by knowing the bit size of the key.

+ 1

"sys.getsizeof(public_key.export_key())" gives that result - size of key including n and e, isn't it??

0

After exporting the key, you can easily find the length or size of key here is an example:
https://code.sololearn.com/cznQED1Y42hC/?ref=app
If output doesn't comes in first run try running multiple times.

0

AKSHAY🇮🇳 when I generate a public key, its length is 624. Why?

0

Yahel what you want to find⁉️
Size or length of characters??

0

AKSHAY🇮🇳, 1 char = 1 byte as far as I know...
I need to transfer the public key over sockets. I need to specify the size in bytes of the public key in order to use the sockets.
For some reason, the length of my public key is 624 even though it's supposed to be 3072/8...
Why?

0

If you want to find the size of your public key then you can use "sys.getsizeof" to get the size.
(I have edited my code.)

0

AKSHAY🇮🇳,
I found out that if I use size_in_bytes() on the rsa key - I get 3072/8, which is what I want.
If I export the key to PEM format, its length grows to 616 bytes. Why? How can fix that?

0

Found this related question: https://stackoverflow.com/questions/49759326/how-to-send-public-key-from-server-to-client-via-socket-using-pickle
I don't think we need to define the size of key to send it over sockets.

0

AKSHAY🇮🇳, it's gives me 649 instead of 616...

0

Yahel I think its due to system on which code is currently running, try on your system.

- 1

write a program to find the average of five numbers. the variable "X" (number to add), "Sum" and"AVG" should be declared using reference variable. use one of the control structure to add the number iteratively, compute the average using the first declaration variable (AVG) and print it using the second variable name (average).