ASP.NET 2.0 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. 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:

|