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.fe.net.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
it might be becz the Fe.net object is unable to decrypt the data because it is not a valid token. There are a few cause for this i guess
Make sure that the key used to initilize the Fe.net obj on the client side is the same as the key used on the server side.
Make sure that the data being passed to the Fe.net obj is in the corct frmt. it seems like you are using base64.urlsafe_b64encode to encode the key, so make sure that the data being passed to the Fe.net object is also in this format.
Make sure that the data is not being modified or corrupted during the transmission between client & server. could be an issue with the.netwrk conxtion or a problem with the JSON formatting.
Check the value of formated_data that is being sent to the server,I think it should be a bytes object not a string.
Make sure that the dumps and loads are using the same format utf-8 or utf-16
see if you are using the correct method of fe.net, you might be using encrypt method instead of decrypt or vice-versa.
Try these steps, see which one resolves the issue.
Here are a few suggestions for improving the code and troubleshooting the
Verify that the key being passed to Fe.net is the correct length and format. The key must be 32 bytes long and ensure it is encoded in base64 URL-safe format. Check that the data being passed to Fe.net for encryption/decryption is also in the correct format. It should be bytes-like and not to be encoded before passing it to Fe.net. Make sure that the same key is being used for encryption and decryption on both the client and the server. If the keys are different, the encryption/decryption process will not work properly. Also check the key is not getting expired during the communication process.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.