1

I'm programming a client-server communication encrypted with RSA, using tcp sockets. I generate public and private keys, but when I want to exchange the public keys between client and server I get this error:

TypeError: must be convertible to a buffer, not PublicKey

This is server code:

import socket
import rsa

print "Generating keys"
(public, private) = rsa.newkeys(1024, poolsize=2)
print "Keys generated."

tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
tcpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
tcpSocket.bind(("0.0.0.0", 1025)) 
tcpSocket.listen(1)

print "Waiting for client..."
(client, (ip, port)) = tcpSocket.accept() 

print "Connection received from: ", ip


client.send(public) #This is where I get the error

I've tried with this line too:

client.send(str(public))

With this I can send the public key, but then I can't use it to encrypt data (because now the public key is a string).

Thank you for your help !

1 Answers1

0

You can use Pickle or cPickle:

import cPickle
to_send=cPickle.dumps(the_key)
sock.send(to_send)

And then unpickle it:

import cPickle
s=sock.recv()
key=cPickle.loads(s)
ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • Just what I needed, thank you. I had a problem pickleing the public key, but I solved it with this post: http://stackoverflow.com/questions/2204155/why-am-i-getting-an-error-about-my-class-defining-slots-when-trying-to-pickl – lapatatandante Apr 01 '15 at 15:31