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?

6th Sep 2021, 7:24 AM
Yahel
18 ответов
+ 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. """
7th Sep 2021, 1:57 PM
AKSHAY🇮🇳
+ 2
I think that 616 bytes is length including n and e
7th Sep 2021, 2:14 PM
AKSHAY🇮🇳
+ 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.
6th Sep 2021, 3:17 PM
Yahel
+ 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
6th Sep 2021, 3:27 PM
AKSHAY🇮🇳
+ 1
AKSHAY🇮🇳, I need to specify because I'm using the 'struct' module.
7th Sep 2021, 11:16 AM
Yahel
+ 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 ?
7th Sep 2021, 2:00 PM
Yahel
+ 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.
7th Sep 2021, 2:18 PM
Yahel
+ 1
"sys.getsizeof(public_key.export_key())" gives that result - size of key including n and e, isn't it??
7th Sep 2021, 3:36 PM
AKSHAY🇮🇳
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.
6th Sep 2021, 2:47 PM
AKSHAY🇮🇳
0
AKSHAY🇮🇳 when I generate a public key, its length is 624. Why?
6th Sep 2021, 3:43 PM
Yahel
0
Yahel what you want to find⁉️ Size or length of characters??
6th Sep 2021, 3:56 PM
AKSHAY🇮🇳
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?
6th Sep 2021, 6:41 PM
Yahel
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.)
7th Sep 2021, 3:26 AM
AKSHAY🇮🇳
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?
7th Sep 2021, 6:27 AM
Yahel
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.
7th Sep 2021, 10:08 AM
AKSHAY🇮🇳
0
AKSHAY🇮🇳, it's gives me 649 instead of 616...
7th Sep 2021, 3:53 PM
Yahel
0
Yahel I think its due to system on which code is currently running, try on your system.
8th Sep 2021, 5:43 AM
AKSHAY🇮🇳
- 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).
7th Sep 2021, 9:21 PM
Obafuwa Abraham Ahmed
Актуальное сегодня
HELP ME