Encryption program for a messenger app
This is not a secure system.

I haven't actually read your code, but unless you have a team of at least 10 security researchers who have been doing nothing other than testing this encryption scheme, you should not use it in production code. You should be using a crypto library built by someone that knows what they are doing. I would recommend cryptography. Docs are here https://cryptography.io/en/latest/. Good luck