BouncyCastle C# DiffieHelman

October 20, 2007


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();


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();


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);
MessageBox.Show(“Test PASSED”, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);

See you to the next post! 🙂

BouncyCastle Experiment Good Results #2

September 27, 2007

Good news from my BouncyCastle Crypto Libs.

I’ve just finished the MultiHasher Experiment, cause a lack of documentation I wasn’t sure of I/O functions but Peter Dettman clarified me something:

for the Byte encoding of the string, could be used the Classical Encoder class of System.ComponentModel, in this way:

InputBuffer = Encod.GetBytes(YourString);

Is used UTF8Encoding instead of ASCII Encoding:

textBox1.Text = Encod.GetString(Hex.Encode(outBuffer));

See you to the next post 🙂

First Experiments with BouncyCastle CryptoLib

September 25, 2007

In these day I’m experimentig a promising library which implements many Crypto Algorithms, called BouncyCastle (which is for .NET, and I’m coding in C#).

Library, seems to be complete and to have good implementations of Common Algorithms, EllipticCurveCryptography, Certifications, OpenPGP, OpenSSL.

A part a little leak of performances in ECIES algorithm, seems to work great.

The big problem is that Hex Conversion functions have some problem, for example Hex.Decode() , fails when the string passed have an odd lenght.

To dayI’ve sent an email to the coders, hope in a fast reply, if i discover how to solve also other minor problems (actually no time to mention all) i’ll post here the fixed piece of code 😉

See you to the next post 🙂

News & Links

September 22, 2007

In these days I’ve searched informations about USB C# Classes and Libraries, because Low Level I/O informations, into .NET is a bit difficult to find, here there are some links that could interest you 😉



DeviceIOControl & USB Using Managed C++ and C#


See you to the next post 🙂