0

I trying implement a simple messaging system with several security features. So I need to send client's RSA key's public key part to server. But I print key on both sides. They are not match.

My client code:

from Crypto.PublicKey import RSA
from Crypto import Random
random_for_key = Random.new().read 
usr_key = RSA.generate(1024, random_for_key) 
usr_pub_pt = usr_key.publickey() 
print(usr_pub_pt)
usr_pub_pt = usr_key.publickey() 
socket2.send(usr_pw_couple.encode())
socket2.send(usr_pub_pt.exportKey(format='PEM', passphrase=None, pkcs=1))

My server code:

r_usr, r_pw = (self.conn.recv(2048).decode().split(" "))
r_pk = RSA.importKey((self.conn.recv(2048).decode()), passphrase=None)
print(r_usr,r_pw)
print(r_pk)

Thank you all.

Şaban Kaya
  • 31
  • 1
  • 8
  • 2
    TCP is a streaming protocol, there are no boundaries when sending data, so you cannot expect to read in the first `recv` exactly what you have sent in the first `send`. – Daniel Dec 10 '17 at 14:46
  • @Daniel: So do you have any idea how I can do this? – Şaban Kaya Dec 10 '17 at 14:49
  • It can be a bit tricky to manage the blocking behavior of `recv`. Just keep in mind that it (a) blocks if there's *nothing* to read, and (b) returns with whatever is available if there's less than requested, or (c) returns the requested amount if there's plenty of data. So you either have to know how much data is coming (and repeatedly call `recv` varying the requested length based on how much you have received), or you have to put a header on the data containing the length, and alternate between reading the header size (to learn the payload size) and reading the amount of data it specifies – lockcmpxchg8b Dec 10 '17 at 17:53

0 Answers0