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:
Click here first ....
Next Bob and Allice will 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:
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.
protected void Button3_Click(object sender, EventArgs e)
{
Random val = new Random();
X.Text = Convert.ToString(val.Next(10));
Y.Text = Convert.ToString(val.Next(10));
G.Text=Convert.ToString(val.Next(50));
N.Text=Convert.ToString(val.Next(200)+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:

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.
|