title: ECDiffieHellmanCng Implementation And Mechanism
body: <p>I've been reading on different cryptography algorithms and I came across the "Diffie Hellman Cryptography Next Generation". I've made a lot of searches about this algorithm and I have  found that it is an Asymmetric cryptography system which is in some ways rivaling RSA. I had a look at the .Net's <code>System.Security.Cryptography</code> and I found it provides Diffie-Hellman cryptography as well. So I wen't to <a href="http://msdn.microsoft.com/en-us/library/vstudio/system.security.cryptography.ecdiffiehellmancng%28v=vs.100%29" rel="nofollow">MSDN</a> and there I found a very nice example on how to use it in C#, but this example has brought some questions into my mind. Let me copy some part of the code first: (I don't post the full code as my question is mainly about this part)</p>
  
  <pre><code>public static byte[] alicePublicKey;
  
  public static void Main(string[] args)
  {
      using (ECDiffieHellmanCng alice = new ECDiffieHellmanCng())
      {
  
          alice.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
          alice.HashAlgorithm = CngAlgorithm.Sha256;
          alicePublicKey = alice.PublicKey.ToByteArray();
          Bob bob = new Bob();
          CngKey k = CngKey.Import(bob.bobPublicKey, CngKeyBlobFormat.EccPublicBlob);
          byte[] aliceKey = alice.DeriveKeyMaterial(CngKey.Import(bob.bobPublicKey, CngKeyBlobFormat.EccPublicBlob));
          byte[] encryptedMessage = null;
          byte[] iv = null;
          Send(aliceKey, "Secret message", out encryptedMessage, out iv);
          bob.Receive(encryptedMessage, iv);
      }
  
  }
  
  private static void Send(byte[] key, string secretMessage, out byte[] encryptedMessage, out byte[] iv)
  {
      using (Aes aes = new AesCryptoServiceProvider())
      {
          aes.Key = key;
          iv = aes.IV;
  
          // Encrypt the message
          using (MemoryStream ciphertext = new MemoryStream())
          using (CryptoStream cs = new CryptoStream(ciphertext, aes.CreateEncryptor(), CryptoStreamMode.Write))
          {
              byte[] plaintextMessage = Encoding.UTF8.GetBytes(secretMessage);
              cs.Write(plaintextMessage, 0, plaintextMessage.Length);
              cs.Close();
              encryptedMessage = ciphertext.ToArray();
          }
      }
  }
  
  }
  </code></pre>
  
  <p>When I look at this, I see that it is the AES that is doing everything, and the <code>ECDiffieHellman</code> is only responsible for providing a key for the AES algorithm. In the meanwhile it itself is based on the SHA256 hashing system.</p>
  
  <p>So my question is, what exactly is this "ECDiffieHellmanCng" doing in this case? What I see is a hybrid cryptography system, one for key exchange and the other for the encryption, this has conflict with what I've read over the internet about this Diffie Hellman algorithm, most sources list it under Asymmetric encryption systems but this example is not showing the same thing. I say this because when I look the <code>AliceKey</code> I see that, it is made based on a Public Key that has been probably made using SHA256 and it itself is not encrypting/ decryption anything.         </p>