---
# System prepended metadata

title: How RSA Work

---

# How RSA Work
Alice has a message (m) which is `35` so `m = 35` need to send and Bob need that message so Alice has to do the following procedure to encrypt the message in a secure way

First Alice choose a `p` and `q` both is a prime number in this case Alice choose `23` and `41` then multiply `p` and `q` it will produce `n` which is a public key now the public key is `943` and compute φ(n) `(p-1)*(q-1)` which now is `880` then we choose `e` and how we choose it `e` must be a positive integer `e` must satisfy the following condition:

- 1 < e < φ(n) (Euler’s totient function with n is p * q) you can read more in [here](https://en.wikipedia.org/wiki/Euler%27s_totient_function) to learn about Euler’s totient function
- e must be a co prime with φ(n) which mean gcd(e, φ(n)) = 1 to calculate `d` the secret exponent

So Alice decide to choose `e = 7` satisfy the condition so now Alice start to calculate with the equation is `c = m ** e mod n` the cipher message which is `c = 35 ** 7 mod 943` and now `c = 545` but now Alice can not send the cipher text and the public key which is `n` to Bob, Alice has to calculate `d` the exponent that only Alice know with the following equation `e * d = 1 mod ((p - 1) * (q - 1))` Alice equation now is `7 * d = 1 mod 880` and we receive `d = 503`  so now to decrypt the cipher text Alice must do the following equation `c ** d mod n` which now is `545 ** 503 mod 943` and the result is `35` 

So how Alice and Bob calculate the encrypted message so both of them has to calculate their own `n` and `e` to send to each other but they can not share their `d` the secret exponent to calculate the secret message Alice send `n_A` and `e_A` to Bob and Bob send back `n_B` and `e_B` Alice calculate secret cipher text with `n_B` and `e_B` then send the cipher text to Bob and he himself calculate that with the `d_B` that only he knows and the way to communicate is done