0

So the message is supposed to be decrypted here :

def recv_message(self):
        while True:
            try:

                data = self.socket.recv(1024)
                # converting values from raw string into dictionary with json
                formatted_data = json.loads(data)

                # check the content of the recived data for the type of state that it is
                if formatted_data["state"] == "keyexchange":

                    # store the public key send from the server in an array
                    # needed to generate the secret for encryption

                    Client.server_public_key.append(int(
                        formatted_data["content"]))

                    # add the secret to the array for the client to generate fernet
                    Client.secret.append(
                        (Client.server_public_key[0] ** Client.c_private_key) % Client.prime)

                    # generate fernet to encrypt and decrypt
                    Client.fernet = Fernet(base64.urlsafe_b64encode(
                        f"{Client.secret[0]:032d}".encode(FORMAT)[:32]))

                if formatted_data["state"] == "message":

                    # standard state of messages
                    # assign all the values to variables from the formatted data locally to be used when necessary
                    address = formatted_data["address"]
                    data = formatted_data["content"]
                    data = self.decrypt(data)

                    # print the message and the address that it came from
                    print(f"{address}: {data}")

            except:
                print(traceback.format_exc())
                print("[CONNECTION ENDED]")
                break

After the server broadcasts the messages back to the clients - This supposed to be a chat app with the CLI

However i get this error

cryptography.fernet.InvalidToken`

Note all messages are formatted into JSON in this code before sending to the server :

` def send_message(self, data, state): # function to send message to server # format all messages through this function try: # make dictionary to format all messages

        if state == "keyexchange":
            d = {"state": state, "address": None, "content": data}
            # dump into a formatted string with json.dumps

            formated_data = json.dumps(d)
        if state == "message":

            # dump into a formatted string with json.dumps
            d = {"state": state, "address": None,
                 "content": str(data)}
            formated_data = json.dumps(d)
    except:
        print(traceback.format_exc())

    # return the formatted values passed into the function
    return self.socket.send(formated_data.encode(FORMAT))

`

Also the value for the key is generated from Diffie Hellman exchange in the recv message function

  • Ive tried converting the data into the correct format using encode and decode - this as far as i can tell isn't the issue
  • Ensured the token is the same on client and server side
  • Ensured the data is being loaded by the JSON formatter
  • Checked that the data is not being altered on either socket server or client
Alfie
  • 1

0 Answers0