Amanda_Panda Amanda_Panda - 1 year ago 103
Python Question

Can't pickle an RSA key to send over a socket

I have a list with a public key and a username that I want to send over a socket.

I found
how to send an array over a socket in python but using pickling won't work either.

My code:

private_key = generateRSA()
public_key = private_key.public_key()
host = ''
port = 8000
username = sys.argv[1]
mySocket = socket.socket()
mySocket.connect((host, port))
dataToSend = [public_key, username.encode()]
dataSend = pickle.dumps(dataToSend)

The error in the console says

dataSend = pickle.dumps(dataToSend)
_pickle.PicklingError: Can't pickle <class '_cffi_backend.CDataGCP'>: attribute lookup CDataGCP on _cffi_backend failed

The key was generated with the

Answer Source

You are trying to send a RSAPublicKey instance, but this is a data structure managed by SSL, not Python. Because you interact with it through a CFFI proxy, you can't pickle one of these.

You'll need to pick a key serialization format to send it instead. You could convert to PEM, for example:

from cryptography.hazmat.primitives import serialization
pem = public_key.public_bytes(

The above would export your key to a string value you could then convert to bytes and send over the socket directly. The other side would load the key from that string again.

Note that the above uses no encryption whatsoever. Anyone intercepting the traffic over that socket would be able to read your public key. That may or may not be a problem for your application.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download