Diffie Hellman Key Exchange algorithm

understanding the Diffie Hellman Key Exchange algorithm with the help of a simple example





The Problem
Suppose there are 2 people, Akbar and Birbal , who wants to use an unsecure channel to decide a shared secret key which will be used for further encrypting a big message. How is this possible ?
As already mentioned the channel is unsecure and there are, say n , number of attackers waiting for the transmission.
The solution is provided by Diffie-Hellman method.
Diffie-Hellman Algorithm
Diffie-Hellman Algorithm is used to decide on a shared secret key that can be used for secret communications while exchanging data over an unsecure channel.The process is explained in detail below :

Steps Akbar Birbal n Attackers
1 Decides a huge prime number P and a generator point G and shares it publically over unsecure channel with Birbal . Receive P and G from Akbar . Intercepts the value of P and G from Akbar .
2 Decides a Secret key a. Decides a Secret key b. Waiting for transmission.
3 Compute Sa Which is
Sa = G a mod P
Compute Sb Which is
Sb = G b mod P
Waiting for transmission.
4 Transmit Sa over unsecure channel. Transmit Sb over unsecure channel Intercepts the value of Sa and Sb .
5 Compute Secret S Which is
S = Sb a mod P which is equivalent to S = (G b mod P ) a mod P which is further equivalent to S = G ba mod P
Compute Secret S Which is
S = Sa b mod P which is equivalent to S = (G a mod P ) b mod P which is further equivalent to S = G ab mod P
Trying to evaluate a and b from Sa and Sb respectively which is next to impossible.
6 Secret shared successfully ready for communication over a unsecure channel. Secret shared successfully ready for communication over a unsecure channel. ----
Example
Let's understand Diffie-Hellman Algorithm with the help of an common example. For the sake of understanding we are using small prime numbers (In actual process, big prime numbers are used). The whole process is given below :

Steps Akbar Birbal n Attackers
1 Let's assume prime number P = 29 and a generator point G = 7 and shares it publically over unsecure channel with Birbal . Receive P = 29 and G = 7 from Akbar . Intercepts the value of P = 29 and G = 7 from Akbar .
2 Decides a Secret key a = 3. Decides a Secret key b = 5. Waiting for transmission.
3 Compute Sa Which is
Sa = G a mod P
Sa = 7 3 mod 29
Sa = 343 mod 29
Sa = 24
Compute Sb Which is
Sb = G b mod P
Sb = 7 5 mod 29
Sb = 16807 mod 29
Sb = 16
Waiting for transmission.
4 Transmit Sa= 24 over unsecure channel. Transmit Sb = 16 over unsecure channel Intercepts the value of Sa=24 and Sb=16 .
5 Compute Secret S Which is
S = Sb a mod P
S = 16 3 mod 29
S = 4096 mod 29
S = 7
Compute Secret S Which is
S = Sa b mod P
S = 24 5 mod 29
S = 7962624 mod 29
S = 7
Trying to evaluate a = ? and b = ? from Sa = 24 and Sb = 16 respectively which is next to impossible.
6 Secret shared successfully ready for communication over a unsecure channel. Secret shared successfully ready for communication over a unsecure channel. ----