0

I am implementing RSA encryption/decryption. The data which is to be encrypted contains different data types. Hence I have used dictionary in python. However I am getting errors. I am attaching code snippet. Please provide solution !! Thank you.

import base64
from cryptography.fernet import Fernet
import os
import hashlib
from cryptography.hazmat.backends import default_backend
from Crypto.PublicKey import RSA
from cryptography.hazmat.primitives.asymmetric import padding
from Crypto import Random


class SplitStore:
....

  def encrPayload(self, payload):

    modulus_length = 256 * 13  # use larger value in production
    privatekey = RSA.generate(modulus_length, Random.new().read)
    public_key = privatekey.publickey()

    private_pem = privatekey.exportKey().decode()
    with open('private_pem.pem', 'w') as pr:
    pr.write(private_pem)

    byte_payload = payload.encode('utf-8', 'strict')
    #print("Byte Payload:{} ".format(byte_payload))

    encrypted_payload = public_key.encrypt(byte_payload, 32)[0]
    #print("type:{}".format(type(encrypted_payload)))
    #print("\nEncrpted METADATA: {}".format(encrypted_payload))

    bchain_file = open("blockchain/file.dat", "wb")
    bchain_file.write((encrypted_payload))

  def createMetaData(self):
    time_stamp = os.stat("/home/pc-01/sample.pdf").st_ctime
    #print("\nTIMESTAMP: {}".format(time_stamp))

    hash_data = hashlib.sha512(self.pdfReader).hexdigest()
    #print("\nHASHDATA : {}".format(hash_data))

    secret_keys = self.j   #j is a list of keys
    #print("List of secret keys: {}".format(self.j))
    #print("String conv: " + str(secret_keys[0]))

    #Creating a dictionary
    payload = {
      "time_stamp" : time_stamp, #int
      "hash" : hash_data, #string
      "keys" : secret_keys #list
    }
    #print("\nPAYLOAD: " + payload)
    return payload 
    ...


def main():

  s1 = SplitStore()
  s1.initializeData()
  ...


if __name__ == "__main__":
  main()

secret_keys in payload is a list containing keys in bytes format. After running the code I get following error:

Traceback (most recent call last): 
....... chunks = self.iterencode(o, _one_shot=True) 
File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode return _iterencode(o, 0) 
File "/usr/lib/python3.5/json/encoder.py", line 179, in default raise TypeError(repr(o) + " is not JSON serializable") 
TypeError: b'Vm3pb7XRJ4W_8M1ShKHAGiuDa2PT1DN_0ncjf0hmNJU=' is not JSON serializable

Is there any way to solve this issue?

shriakhilc
  • 2,922
  • 2
  • 12
  • 17
Bhushan Mahajan
  • 131
  • 3
  • 13
  • 1
    You haven't mentioned what errors you are facing, and at which lines – shriakhilc May 16 '19 at 07:16
  • secret_keys in payload is list containing keys in bytes format. after running the code i get following error: Traceback (most recent call last): ....... chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode return _iterencode(o, 0) File "/usr/lib/python3.5/json/encoder.py", line 179, in default raise TypeError(repr(o) + " is not JSON serializable") TypeError: b'Vm3pb7XRJ4W_8M1ShKHAGiuDa2PT1DN_0ncjf0hmNJU=' is not JSON serializable it is saying bytes are not JSON serializable. – Bhushan Mahajan May 16 '19 at 07:26
  • Can anyone please share logic or method to implement RSA on python dictionaries – Bhushan Mahajan May 16 '19 at 07:29
  • You might want to check out [this answer](https://stackoverflow.com/a/10872643/6698642). Use `type()` to confirm that the objects are actually JSON serializable, and if not, then cast them to types that are. – shriakhilc May 16 '19 at 08:53
  • Also, while your error message isn't `TypeError: Object of type bytes is not JSON serializable`, they are usually not serializable. So can you try converting the `bytes` objects to a normal string and then try? You need to use `bytes.decode()`, with "utf-8" encoding I assume. – shriakhilc May 16 '19 at 09:04
  • i converted the bytes keys into string. However after decryption I am not able to convert string keys into bytes again. I am sorry I am not a python guy much ! – Bhushan Mahajan May 16 '19 at 09:12
  • You can use the corresponding `encode()` method for that. – shriakhilc May 16 '19 at 09:14

1 Answers1

1

If the problem isn't with the bytes as described below, you can narrow down on what causes the issue by following this answer and figuring out which of your variables are not serializable.


Your items in secret_keys are bytes, which are not JSON serializable. So you'll have to convert them to normal str objects. You can do so via the decode method as follows:

plainstr = bytestr.decode("utf-8") 
# The encoding passed need not be "utf-8", but it is quite common

Similarly, you can encode a str to bytes as follows:

encodedstr = plainstr.encode("utf-8")
shriakhilc
  • 2,922
  • 2
  • 12
  • 17