I've got a problem. I'm using pycrypto and rsa. I want to generate my rsa keys. Then I want to send my public key (in binary or base64 or similar) but first I want to encrypt it with server public. Because I want to be sure that no one is sniffing and my public needs to be not well known.
And then the problem starts. Because when I'm encrypting my public key, after decrypting it, decrypted data is unreadable.
And I have no idea why. When I exchange publics and send normal data (not public keys) encrypting works. I can't find sollution to how send my public encrypted.
Can someone help me? Every comment will be useful
My code:
random_generator = Random.new().read
self.private_key = RSA.generate(1024, random_generator)
self.public_key = self.private_key.publickey()
keytoexport =self.public_key.exportKey(format='PEM', passphrase=None)
#client encrypting to server
def _encrypt(self, content):
return self.server_public_key.encrypt(content, 32)
#server decrypting content
def _decrypt(self, content):
return self.parent.private_key.decrypt(content)
Im sending datagram by client like that.
def send_datagram(self, datagram):
datagram = pickle.dumps(datagram)
self.socket.sendall(datagram)
response_server = self.socket.recv(2048)
return pickle.loads(response_server)
And retriving it to server like that.
receive_socket = self.request
ask = receive_socket.recv(2048).strip()