将pynacl中的公钥作为JSON对象发送

Awn*_*Awn 4 python sockets json nacl-cryptography

我有一个服务器和客户端设置,当客户端连接时,它通过一个带有用户名和 public_key 的 JSON 对象发送。

我目前的代码:

private_key = PrivateKey.generate()
public_key = private_key.public_key
payload = json.dumps({"username": username, "public_key": public_key}).encode('hex')
Run Code Online (Sandbox Code Playgroud)

但我得到:

TypeError: <nacl.public.PublicKey object at 0x7fc6ecff18d0> is not JSON serializable
Run Code Online (Sandbox Code Playgroud)

任何解决方案?

小智 6

这可能有点晚了,但已在此处解释

例如,如果您希望将其作为 Base64,则可以

from nacl.encoding import Base64Encoder
print(public_key.encode(Base64Encoder).decode())
Run Code Online (Sandbox Code Playgroud)