Saturday, 15 August 2015

How to use BouncyCastle's Diffie-Hellman in C#? -


I am writing an app that exchanges data between a phone and a Windows PC, and I I want to protect Duffy-Hellman is sent with the key generated from the exchange.

I am trying to use BouncyCastle for this, but almost non-existent documents of C # implementation have been stumped to me.

What do I want to know: What is the workflow for creating a DH key and computing a shared key when the key on the other side is received? (I'm assuming that I can send my key in the form of a string and I can work with the string on the other side. What object / method do I use in C # for that object?

OK, after a lot of testing, I had to do this work.

I believe the reader (1) knows what is Diffie-Hellman and what is useful for it (read for details) and (2) has already imported Bondi Caste NuGet Through .NET project.

You will need to import:

  using Org.BouncyCastle.Crypto; By using the organization. Barsty castell Crypto Generator; By using the organization. Barsty castell Crypto Parameter; By using the organization. Barsty castell Security;  

How to generate G and P:

  Generate public DHParameters parameter () {var generator = new DHParametersGenerator (); Generator.init (bits, defaultformpurbity, new securendum ()); Return generator GenerateParameters (); }  

What to get in the form of string and get p?

  public string GetG (DHParameters parameter) {return parameters.G.ToString (); } Public string GetP (DHParameters parameter) {return parameter. P. Toasting (); }  

How to prepare A and A:

  Public asymmetric seafarer generator keys (DHPMeters parameters) {var keyGen = GeneratorUtilities.GetKeyPairGenerator ("DH") ; Var kgp = New DHKeyGenerationParameters (New SecureRendame (), Parameter); KeyGen.Init (KGP); Return keygain GenerateCapier (); }  

Want to read A and A as a string?

  // This gets a public string. As DHPublicKeyParameters; If (dhPublicKeyParameters! = Null) {return dhPublicKeyParameters.Y.ToString (); } Throw new NullReferenceException ("The available key pair is not a valid DH Chinayer."); } // This gives a public string GetPrivateKey (AsymmetricCipherKeyPair keyPair) {var dhPrivateKeyParameters = _GeneratedKey.Private DHPrivateKeyParameters; If (dhPrivateKeyParameters! = Null) {return dhPrivateKeyParameters.X.ToString (); } Throw new NullReferenceException ("The available key pair is not a valid DH Chinayer."); }  

To import parameters from the string just do the following:

Parameter = new DHParameters (p, g);

B and B to generate only generate parameters instead of GenerateKeys () importedParameters Use Please.

You say that you produce B and B. And already got P, G and A. To calculate the secret shared:

  public BigInteger ComputeSharedSecret (string A, Simetrikkeepametr Beepiwaiteke, Diacpiramets internal parameter) {var importedKey = new DHPublicKeyParameters (New Bigintejr (A), internal parameters) ; Var internal thread = agreement availability Getbasic Consent ("DH"); InternalKeyAgree.Init (bPrivateKey); Internal Internal Return Calculate (imported); }  Repeat for 

A and now have a shared secret between the two clients, which is designed to encrypt communications.

Hope this is useful.


No comments:

Post a Comment