Diffie-Hellman ExampleDiffie-Hellman is a standard method of Alice and Bob being able to communicate, and end up with the same secret encryption key. It is used in many applications, and uses two numbers (G and N) for the first part of the calculation (of which N must be a prime number): [Related Lecture] [Tutorial] [Software Tutorial][Software Lecture] [Theory][Blog] [Picking G value]
Next Bob and Alice will generate two random numbers (X and Y), calculate an X value and a Y value, respectively:
and Bob will send his A value to Alice, and Alice will send her B value to Bob, and they now re-calculate the values to generate the same shared key:
|
Try an example
- x=3, y=4, G=4 and N=7. Try
- x=6, y=15, G=5 and N=23. Try Refer
- x=5, y=7, G=10 and N=541. Try Refer
- x=6, y=15, G=5 and N=23. Share=2. Try Refer
- x=7, y=7, G=5 and N=11. Try
- x=7, y=9, G=8 and N=13. Try
- x=5, y=4, G=2969 and N=9929. Share=8106. Try
- x=6, y=5, G=3881 and N=125. Share=792. Try
- x=3, y=4, G=3623 and N=1153. Share=939. Try
Outline
In Diffie-Hellman, Bob and Alice agree on G (a generator) and N (a prime number), and then Bob picks a random value of x, and Alice picks a random value of y:
Bob (x) | Alice (y) |
\(b=G^x \mod N\) | \(a=G^y \mod N\) |
Bob sends Alice the value of b | Alice sends Bob the value of a |
\(Key=a^x \mod N\) | \(Key=b^y \mod N\) |
Presention
Code Used
public ActionResult diffie(hashing h, string button1, string button2, string button3) { if (button1 != null) { SecureRandom random = new SecureRandom(); int x = Global.random(20) + 2; int y = Global.random(20) + 2; int G = getPrime(); int N = getPrime(); BigInteger g = new BigInteger(Convert.ToString(G)); BigInteger n = new BigInteger(Convert.ToString(N)); BigInteger A = g.Pow(x).Mod(n); BigInteger B = g.Pow(y).Mod(n); ViewData["GValue"] = g.ToString(); ViewData["Nvalue"] = n.ToString(); ViewData["XValue"] = Convert.ToString(x); ViewData["Yvalue"] = Convert.ToString(y); BigInteger k1 = B.Pow(x).Mod(n); BigInteger k2 = A.Pow(y).Mod(n); ViewData["Avalue"] = A.ToString(); ViewData["Bvalue"] = B.ToString(); ViewData["BobKey"] = k1.ToString(); ViewData["AliceKey"] = k2.ToString(); Session["GValue"] = ViewData["GValue"]; Session["Nvalue"] = ViewData["NValue"]; Session["XValue"] = ViewData["XValue"]; Session["Yvalue"] = ViewData["YValue"]; Session["AValue"] = ViewData["AValue"]; Session["Bvalue"] = ViewData["BValue"]; Session["BobKey"] = ViewData["BobKey"]; Session["AliceKey"] = ViewData["AliceKey"]; if (k1.ToString() == k2.ToString()) h.message = "Success ... the keys match!!!"; else h.message = "Failed ... probably due to math overload"; Session["Message"] = h.message; return PartialView("PartialViewGN"); } if (button2 != null) { ViewData["GValue"] = Session["GValue"]; ViewData["Nvalue"] = Session["NValue"]; ViewData["XValue"] = Session["XValue"]; ViewData["Yvalue"] = Session["YValue"]; ViewData["AValue"] = Session["AValue"]; ViewData["Bvalue"] = Session["BValue"]; return PartialView("PartialViewXY"); } if (button3 != null) { ViewData["GValue"] = Session["GValue"]; ViewData["Nvalue"] = Session["NValue"]; ViewData["XValue"] = Session["XValue"]; ViewData["Yvalue"] = Session["YValue"]; ViewData["AValue"] = Session["AValue"]; ViewData["Bvalue"] = Session["BValue"]; ViewData["BobKey"] = Session["BobKey"]; ViewData["AliceKey"] = Session["AliceKey"]; ViewBag.Message = Session["Message"]; return PartialView("PartialViewGenK"); } return View(); }
Diagram
|