BouncyCastle C# DiffieHelman

Hi,

I’ve done some experiments with DiffieHelman Basic Agreement, code is written in C# using as Crypto Framework

Org.BouncyCastle.Crypto.Agreement.DHBasicAgreement DHB = new Org.BouncyCastle.Crypto.Agreement.DHBasicAgreement();

BigInteger g = new BigInteger(“153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc”, 16);
BigInteger p = new BigInteger(“9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b”, 16);

DHParameters dhParams = new DHParameters(p,g);
DHKeyGenerationParameters dhkParam = new DHKeyGenerationParameters(new SecureRandom(),dhParams);
DHKeyPairGenerator kpGen = new DHKeyPairGenerator();

kpGen.Init(dhkParam);

AsymmetricCipherKeyPair pair = kpGen.GenerateKeyPair();

//First Pair

DHPublicKeyParameters pub1 = (DHPublicKeyParameters) pair.Public;
DHPrivateKeyParameters pvt1 = (DHPrivateKeyParameters)pair.Private;

textBox1.Text = pvt1.X.ToString();
textBox2.Text = pub1.Y.ToString();

// Second KeyPair Generation

DHPublicKeyParameters pub2 = (DHPublicKeyParameters)pair.Public;
DHPrivateKeyParameters pvt2 = (DHPrivateKeyParameters)pair.Private;

DHBasicAgreement e1 = new DHBasicAgreement();
DHBasicAgreement e2 = new DHBasicAgreement();

e1.Init(pvt1);
e2.Init(pvt2);

BigInteger k1 = e1.CalculateAgreement(pub2);
BigInteger k2 = e2.CalculateAgreement(pub1);

if (!k1.Equals(k2))
MessageBox.Show(“Bit 2-way test FAILED”, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);
else
MessageBox.Show(“Test PASSED”, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);

See you to the next post!🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: