2

I'm using the python rsa module to generate a public/private key pair. I want to send the public key to the other computer through a socket connection.

When I try to encode the public key to send it, I get this error:

  File "chatclient.py", line 128, in <module>
    s.sendall(pubkey.encode('utf-8'))
AttributeError: 'PublicKey' object has no attribute 'encode'

I cannot figure out a way to encode the key other than the method that causes the error. If I try to convert it to a string, encode it and send it through, I cannot use the key to encrypt any messages, nor are there any documented ways to turn it back into a PublicKey object.

This is what causes the error:

s.sendall(pubkey.encode('utf-8'))

Here's the package on pypi and the documentation:

https://pypi.org/project/rsa/

https://stuvel.eu/python-rsa-doc/usage.html

4
  • What package do you use here? Where is the PublicKey class defined? Commented Aug 4, 2019 at 10:09
  • the .encode is usually a string thing what type of object is pubkey? check to see if it already haves a bytes or to_bytes method Commented Aug 4, 2019 at 10:13
  • @Nullman I've checked at stuvel.eu/python-rsa-doc/reference.html, and the class rsa.PublicKey doesn't have anything like that. Commented Aug 4, 2019 at 10:21
  • You should be able to save the key as a string with save_pkcs1 and load it from said string with load_pkcs1. Commented Aug 4, 2019 at 11:01

1 Answer 1

5

Use save_pkcs1 and load_pkcs1:

a = pubkey.save_pkcs1(format='DER')
b = rsa.key.PublicKey.load_pkcs1(a, format='DER')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.