Why does $(g^a \bmod n)^b = (g^b \bmod n)^a = g^{ab} \bmod n $? The Diffie–Hellman key exchange protocol relies on the fact that one person, Alice, can perform $(g^a \bmod n)^b $ and another person, Bob, can perform $(g^b \bmod n)^a $ and they will both arrive at the same number: $g^{ab} \bmod n $. This allows Alice and Bob to create a private, yet symmetric, key. Why is this identity true?

Because gb mod n=(g mod n)b mod n 
Here is its proof by induction.
Base Case: g1 mod n=(g mod n)1 mod n { Because 0<=(g mod n)<n }
lets show for g2 mod n= (g . g ) mod n =((g mod n).(g mod n) )mod n=(g mod n)2 mod n { Because of Multiplicative Property}
Lets prove for gk+1 mod n=(g . gk)mod n=(g mod n . gk mod n)mod n=((g mod n). ( g mod n )k mod n) mod n=((g mod n) mod n. ( g mod n )k mod n) mod n=
(g mod n)k+1 mod n. 
Hence Proved.
