Implementing End to End Encryption in your Cross Platform App
Tags: security, encryption, cryptography, crossplatform
Published: 2021-02-14 03:19:10

This is a two part tutorial series for implementing End to End Encryption. If you haven't read the [first part](https://dev.to/ruppysuppy/implementing-end-to-end-encryption-in-your-cross-platform-app-3a2k) 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](https://dev.to/ruppysuppy/implementing-end-to-end-encryption-in-your-cross-platform-app-3a2k).

# 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.

```python
import requests

BASE_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
assert alice_shared_key == bob_shared_key
print(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.

```python
def xor_encrypt_decrypt(message: str, key_string: str):
    key = list(key_string)
    output = []
    for i in range(len(message)):
        char_code = ord(message[i]) ^ ord(key[i % len(key)][0])
        output.append(chr(char_code))
    return "".join(output)


def encrypt(message: str, key: str):
    return xor_encrypt_decrypt(message, key)


def decrypt(encrypted_message: str, key: str):
    return xor_encrypt_decrypt(encrypted_message, key)
```

# Testing out the **End to End Encryption**

It always a good practice to test out your code. We can test it out using the following snippet

```python
message = "Random message"
encrypted_message = encrypt(message, alice_shared_key)

print(encrypted_message)

decrypted_message = decrypt(encrypted_message, bob_shared_key)

print(decrypted_message)
```

# Project using this Implementation

**Smartsapp**

Web-app: [https://smartsapp-ba40f.firebaseapp.com](https://smartsapp-ba40f.firebaseapp.com/)

{% github https://github.com/ruppysuppy/SmartsApp %}

Finding **personal finance** too intimidating? Checkout my **Instagram** to become a [**Dollar Ninja**](https://www.instagram.com/the.dollar.ninja/)

# Thanks for reading

Reach out to me on:

-   [Portfolio](https://tapajyoti-bose.web.app)
-   [GitHub](https://github.com/ruppysuppy)
-   [LinkedIn](https://www.linkedin.com/in/tapajyoti-bose-429a601a0)