1
votes

I am sending data in Json format between two nodes using Python Requests module functionality on the client side and Flask server on the server side. I need to digitally sign a Python 2.7 string "some string" with an RSA private key and verify the signature on the server side. I have chosen the Pycryptodome module for this purpose. Unfortunately, json.dumps() [from the Json Python module] requires that the signed hash of the string be converted from the form that Pycryptodome's 'pss' signature function outputs (a byte string) to a unicode string -- json requires unicode: How to encode bytes in JSON? json.dumps() throwing a TypeError

I am using the base 64 module's base64.b64encode() funtion to transform the digital signature into a form that json.dumps() can process -- the signature is put into a python dictionary with some other stuff before jsonoficatoin -- and decoding on the server side with base64.b64decode in hopes of getting back a byte string that can be processed by the verify() function of Pycryptodome.

# client
hash = SHA256.new(some_string)
sig = pss.new(PrivKey).sign(hash)
finicky_Json = base64.b64encode(sig)

...

# server
hash = SHA256.new(some_string)
back_to_normal_sig = base64.b64decode(finicky_Json)
public_key_object = pss.new(PubKey)
public_key_object.verify(hash, back_to_normal_sig)`

Unfortunately, I am get in a type error:

raise TypeError("Object type %s cannot be passed to C code" % type(data)) TypeError: Object type <type 'unicode'> cannot be passed to C code What am I missing? Thank You

1

1 Answers

0
votes

If you the contents you are passing on is ASCII (which it should be) you can wrap the unicode string using str().