This is a two part tutorial series for implementing End to End Encryption. If you haven't read the first part you are suggested read it before continuing. The second part will be focusing on encrypting any message between two users using the shared key generated in the first part.
Quick Recap
In the last blog we got a server up and running to generate the shared key using Diffie Hellman algorithm.
Generating the shared key
We would be using the requests module to fetch the keys. Let's generate the shared keys.
importrequestsBASE_URL="http://127.0.0.1:5000"# assuming the server is running locally
alice=requests.get(f"{BASE_URL}/generate-keys").json()alice_private,alice_public=alice["private_key"],alice["public_key"]bob=requests.get(f"{BASE_URL}/generate-keys").json()bob_private,bob_public=bob["private_key"],bob["public_key"]alice_params={"local_private_key":alice_private,"remote_public_key":bob_public}bob_params={"local_private_key":bob_private,"remote_public_key":alice_public}alice_shared_key=requests.get(f"{BASE_URL}/generate-shared-key",params=alice_params).json()["shared_key"]bob_shared_key=requests.get(f"{BASE_URL}/generate-shared-key",params=bob_params).json()["shared_key"]# alice and bob have access to the same shared key
assertalice_shared_key==bob_shared_keyprint(alice_shared_key)
Adding the Cipher
Now that we have the shared keys, need a cipher to encode messages using the key. Let's use XOR Cipher for this purpose as it's easy to implement and secure as well.