title: Cryptopp Ephemeral Diffie Hellman to C# Bouncy Castle
body: <p>I'm trying to convert cryptopp DiffieHellman 2 key agreement method to c# bouncy castle library.</p>
  
  <p>Here is the help page about cryptoo c++ library: <a href="http://www.cryptopp.com/wiki/Diffie-Hellman" rel="nofollow">http://www.cryptopp.com/wiki/Diffie-Hellman</a></p>
  
  <p>I'm trying to implement RFC 5114's 1024-bit MODP group to C#.</p>
  
  <p>But there are a lot of problems I couldn't solve.</p>
  
  <ol>
  <li><p>When keyPair generates a key, it is 131 bytes, but it must be 128 bytes, because server sending to me 256 bytes key with static and ephemeral key. I must send 256 bytes too .But 1-, 2-, and 3-byte values are static in every key so I'm removing first 3 bytes in keys is it true?</p></li>
  <li><p>Which secret key must I use for converting shared secret to other encryption system keys?</p></li>
  <li><p>Example I have a secret key, how can I convert it for Twofish, RC6, xTEA, Serpent etc?</p></li>
  </ol>
  
  <p>Here is my code:</p>
  
  <pre><code>    public byte[] CreateaNewDiffieHellmanKey()
      {
  public static string Phex = "B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C69A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C013ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD7098488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708DF1FB2BC2E4A4371";
  public static string Ghex = "A4D1CBD5C3FD34126765A442EFB99905F8104DD258AC507FD6406CFF14266D31266FEA1E5C41564B777E690F5504F213160217B4B01B886A5E91547F9E2749F4D7FBD7D3B9A92EE1909D0D2263F80A76A6A24C087A091F531DBF0A0169B6A28AD662A4D18E73AFA32D779D5918D08BC8858F4DCEF97C2A24855E6EEB22B3B2E5";
  public static string Qhex = "F518AA8781A8DF278ABA4E7D64B7CB9D49462353";
  
          BigInteger P = new BigInteger(Phex, 16);
          BigInteger G = new BigInteger(Ghex, 16);
          BigInteger Q = new BigInteger(Qhex, 16);
  
          IAsymmetricCipherKeyPairGenerator staticKeyGen = GeneratorUtilities.GetKeyPairGenerator("DH");
          IAsymmetricCipherKeyPairGenerator ephemeralKeyGen = GeneratorUtilities.GetKeyPairGenerator("DH");
  
          DHParameters dhParams = new DHParameters(P, G, Q, 0, 160);
  
          DHP = dhParams;
  
          KeyGenerationParameters kgpSt = new DHKeyGenerationParameters(new SecureRandom(), dhParams);
          KeyGenerationParameters kgpEp = new DHKeyGenerationParameters(new SecureRandom(), dhParams);
          staticKeyGen.Init(kgpSt);
          ephemeralKeyGen.Init(kgpEp);
  
          AsymmetricCipherKeyPair staticKeyPayir  = staticKeyGen.GenerateKeyPair();
          staticKeyEgri = AgreementUtilities.GetBasicAgreement("DH");
          staticKeyEgri.Init(staticKeyPayir.Private);
  
          AsymmetricCipherKeyPair ephemeralKeyPair  = ephemeralKeyGen.GenerateKeyPair();
          ephemeralKeyEgri = AgreementUtilities.GetBasicAgreement("DH");
          ephemeralKeyEgri.Init(staticKeyPayir.Private);
  
          AsymmetricKeyParameter StaticPublicKey = staticKeyPayir.Public;
          SubjectPublicKeyInfo StaticPublicKeyinfomuz = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(StaticPublicKey);
          byte[] st1 = StaticPublicKeyinfomuz.PublicKeyData.GetBytes();
          byte[] staticPublic = new byte[128];
  
          Array.Copy(st1, 3, staticPublic, 0, staticPublic.Length);
  
          AsymmetricKeyParameter EphPublicKey = staticKeyPayir.Public;
          SubjectPublicKeyInfo EphPublicKeyinfomuz = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(EphPublicKey);
          byte[] ep1 = StaticPublicKeyinfomuz.PublicKeyData.GetBytes();
          byte[] ephemeralPublic = new byte[128];
  
          Array.Copy(ep1, 3, ephemeralPublic, 0, ephemeralPublic.Length);
  
          return Bot.Birleştir(staticPublic, ephemeralPublic); // Combine 2 byte arrays
  
      }
  
  
      public bool AgreeTheKey(byte[] buffer)
      {
  
              byte[] staticpublic = new byte[128];
              byte[] ephemeralpublic = new byte[128];
  
              Array.Copy(buffer, 0, staticpublic, 0, staticpublic.Length);
              Array.Copy(buffer, staticpublic.Length, ephemeralpublic, 0, ephemeralpublic.Length);
  
              ICipherParameters istatic = new KeyParameter(staticpublic);
              ICipherParameters iphemeral = new KeyParameter(ephemeralpublic);
  
              DHPublicKeyParameters dhkpST = new DHPublicKeyParameters(new BigInteger(staticpublic), DHP);
              DHPublicKeyParameters dhkpEP = new DHPublicKeyParameters(new BigInteger(staticpublic), DHP);
  
              SharedStatic = staticKeyEgri.CalculateAgreement(dhkpST).ToByteArray();
              SharedEphemeral = ephemeralKeyEgri.CalculateAgreement(dhkpEP).ToByteArray();
  
              byte[] Sharedkey = SharedStatic;
  
  
            return true;
  
  
  
      }
  </code></pre>