# Diffie-Hellman Example

Diffie-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. Initially the values of G and N are defined:

G:

N:

Next Bob and Allice will calculate an X value and a Y value, respectively:

 Bob X Value Alice's Y value Next ... Bob's A value Alice's B value A=G^x mod N B=G^y mod N

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:

 Finally ... Bob's Key Alice's Key Key=B^x mod N Key=A^y mod N

Then, as if by magic Bob and Alice have the same secret key. Obviously this example uses small 64-bit integers, but it shows the principle.

```using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void Button3_Click(object sender, EventArgs e)
{
Random val = new Random();

X.Text = Convert.ToString(val.Next(10)+2);
Y.Text = Convert.ToString(val.Next(10)+2);
G.Text=Convert.ToString(val.Next(40)+5);
N.Text=Convert.ToString(val.Next(100)+10);
}
protected void Button1_Click(object sender, EventArgs e)
{
double g, n, x, y;
long a, b;

g = Convert.ToDouble(G.Text);
n = Convert.ToDouble(N.Text);
x = Convert.ToDouble(X.Text);
y = Convert.ToDouble(Y.Text);

a = Convert.ToInt64(Math.Pow(g, x)) % Convert.ToInt64(n);
b = Convert.ToInt64(Math.Pow(g, y)) % Convert.ToInt64(n);

A.Text = Convert.ToString(a);
B.Text = Convert.ToString(b);
}
protected void Button2_Click(object sender, EventArgs e)
{

double g, n, x, y;
long a, b;

g = Convert.ToDouble(G.Text);
n = Convert.ToDouble(N.Text);
x = Convert.ToDouble(X.Text);
y = Convert.ToDouble(Y.Text);
a = Convert.ToInt64(A.Text);
b = Convert.ToInt64(B.Text);

this.BobKey.Text = Convert.ToString(Convert.ToInt64(Math.Pow(b, x)) % Convert.ToInt64(n));
this.AliceKey.Text = Convert.ToString(Convert.ToInt64(Math.Pow(a, y)) % Convert.ToInt64(n));
}
}```

and an example:3" height="409" />

Other related .NET articles I've written include:

- Design Tip 298. [.NET] HMAC-SHA1.
- Design Tip 243. [.NET] Base-64 or Hex hash values.
- Design Tip 242. [.NET] Digital Certificates.
- Design Tip 241. [.NET] Public-key Encryption.
- Design Tip 240. [.NET] Diffie-Hellman Method.
- Design Tip 239. [.NET] Symmetric Encryption (Private-key).
- Design Tip 238. [.NET] Obfuscation Part II.
- Design Tip 237. [.NET] Obfuscation Part I
- Design Tip 236. [.NET] Data packet capture (filters: IP, TCP, and so on).
- Design Tip 235. [.NET] Data packet capture.
- Design Tip 234. [.NET] Interface to network adapter.
- Design Tip 232. [.NET] Creating an SSH client.
- Design Tip 231. [.NET] Creating an SNMP client.
- Design Tip 216. [.NET] Client/server communications.
- Design Tip 210. [XML/.NET] XML and .NET.
- Design Tip 207. [.NET] Treeviews for interest.
- Design Tip 206. [.NET/Design] Design, evaluate, design, .....
- Design Tip 205. [.NET] Treeviews.
- Design Tip 203. [.NET] Replacing menus with Treeviews.
- Design Tip 202. [.NET/Flash] .NET and Flash - the perfect pair.