title: Asymmetric cryptography example in C#
body: <p>I need to send confidential data to a server over a TCP connection. I have done a lot of researching and I understand the theoretical part. Based on what I have researched I want to do the following:</p>
  
  <p>Note there is a server and a client: (we assume that public keys of either the client or server can be obtain by anyone)</p>
  
  <ol>
  <li><p>client creates his public and private key. He is able to encrypt with his private key and decrypt with his public key.</p></li>
  <li><p>server creates his public and private keys. private key is used to decrypt messages and public key is used to encrypt messages. (note is the other way around as with the client)</p></li>
  <li><p>the client get's the server's public key. client then will be able to encrypt messages with that key and the only one that will be able to decrypt that message would be the server's private key.</p></li>
  <li><p>since the server needs to be certain that the message comes from that specific client then the client will encrypt his name (signature) with his private key. </p></li>
  <li><p>so the client message will contain:  data to be send, client's public key, client name encrypted with the client's private key.</p></li>
  <li><p>the client will encrypt the message with the public key from the server. client will then send that message to the server.</p></li>
  <li><p>the server will decrypt the message it just received with his private key. </p></li>
  <li><p>once the message is decrypted it will contain the data (info), encrypted signature, public key from client.</p></li>
  <li><p>finally, the server will decrypt the client signature with the public key that was contained on the message to verify that the message is from that client.</p></li>
  </ol>
  
  <hr>
  
  <p>OK so this is how asymmetric cryptography works. I have also researched about the classes that enable you to create this key pairs with the .NET framework.  The classes that I researched that enable you do create this public and private key pairs are:</p>
  
  <pre><code>System.Security.Cryptography.DES
  System.Security.Cryptography.DSACryptoServiceProvider 
  System.Security.Cryptography.ECDsa 
  System.Security.Cryptography.ECDsaCng 
  System.Security.Cryptography.ECDiffieHellman 
  System.Security.Cryptography.ECDiffieHellmanCng 
  System.Security.Cryptography.RSA 
  System.Security.Cryptography.RSACryptoServiceProvider 
  </code></pre>
  
  <p>so now my problems comes on how do I use one of this classes to do it with C#? I understand how the theoretical part works but how do I do what I just described with code. I have researched for some examples but I am having a hard time understanding them. </p>
  
  <p>here is one example that I found that I believe does what I described:</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Security.Cryptography;
  using System.Text;
  
  namespace Example
  {
      class Program
      {
          static CngKey aliceKey;
          static CngKey bobKey;
          static byte[] alicePubKeyBlob;
          static byte[] bobPubKeyBlob;
  
          static void Main()
          {
              CreateKeys();
              byte[] encrytpedData = AliceSendsData("secret message");
              BobReceivesData(encrytpedData);
  
              Console.Read();
  
          }
  
          private static void CreateKeys()
          {
              aliceKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256);
              bobKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256);
              alicePubKeyBlob = aliceKey.Export(CngKeyBlobFormat.EccPublicBlob);
              bobPubKeyBlob = bobKey.Export(CngKeyBlobFormat.EccPublicBlob);
          }
  
          private static byte[] AliceSendsData(string message)
          {
              Console.WriteLine("Alice sends message: {0}", message);
              byte[] rawData = Encoding.UTF8.GetBytes(message);
              byte[] encryptedData = null;
  
              using (var aliceAlgorithm = new ECDiffieHellmanCng(aliceKey))
              using (CngKey bobPubKey = CngKey.Import(bobPubKeyBlob,
                    CngKeyBlobFormat.EccPublicBlob))
              {
                  byte[] symmKey = aliceAlgorithm.DeriveKeyMaterial(bobPubKey);
                  Console.WriteLine("Alice creates this symmetric key with " +
                        "Bobs public key information: {0}",
                        Convert.ToBase64String(symmKey));
  
                  using (var aes = new AesCryptoServiceProvider())
                  {
                      aes.Key = symmKey;
                      aes.GenerateIV();
                      using (ICryptoTransform encryptor = aes.CreateEncryptor())
                      using (MemoryStream ms = new MemoryStream())
                      {
                          // create CryptoStream and encrypt data to send
                          var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
  
                          // write initialization vector not encrypted
                          ms.Write(aes.IV, 0, aes.IV.Length);
                          cs.Write(rawData, 0, rawData.Length);
                          cs.Close();
                          encryptedData = ms.ToArray();
                      }
                      aes.Clear();
                  }
              }
              Console.WriteLine("Alice: message is encrypted: {0}",
                    Convert.ToBase64String(encryptedData)); ;
              Console.WriteLine();
              return encryptedData;
          }
  
          private static void BobReceivesData(byte[] encryptedData)
          {
              Console.WriteLine("Bob receives encrypted data");
              byte[] rawData = null;
  
              var aes = new AesCryptoServiceProvider();
  
              int nBytes = aes.BlockSize &gt;&gt; 3;
              byte[] iv = new byte[nBytes];
              for (int i = 0; i &lt; iv.Length; i++)
                  iv[i] = encryptedData[i];
  
              using (var bobAlgorithm = new ECDiffieHellmanCng(bobKey))
              using (CngKey alicePubKey = CngKey.Import(alicePubKeyBlob,
                    CngKeyBlobFormat.EccPublicBlob))
              {
                  byte[] symmKey = bobAlgorithm.DeriveKeyMaterial(alicePubKey);
                  Console.WriteLine("Bob creates this symmetric key with " +
                        "Alices public key information: {0}",
                        Convert.ToBase64String(symmKey));
  
                  aes.Key = symmKey;
                  aes.IV = iv;
  
                  using (ICryptoTransform decryptor = aes.CreateDecryptor())
                  using (MemoryStream ms = new MemoryStream())
                  {
                      var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write);
                      cs.Write(encryptedData, nBytes, encryptedData.Length - nBytes);
                      cs.Close();
  
                      rawData = ms.ToArray();
  
                      Console.WriteLine("Bob decrypts message to: {0}",
                            Encoding.UTF8.GetString(rawData));
                  }
                  aes.Clear();
              }
          }
      }
  }
  </code></pre>
  
  <p>In this program I believe the client is Alice and the server is Bob.  I have to split this program into two parts. I am having a hard time understanding it and if I give it a try most likely I will make it work. Anyways how can I split this program into a server side code and client side code. I know how to send bytes between server and client. But I don't want to make it work without understanding what is going on. maybe you guys can show me an easier example. </p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>I managed to separate the code: here is the server code (the ip address of my computer happened to be 192.168.0.120) :</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Net.Sockets;
  using System.Net;
  using System.Security.Cryptography;
  using System.IO;
  
  
  namespace ServerListener
  {
      class Program
      {
          static TcpListener server;
  
  
          //static CngKey aliceKey;
          static CngKey bobKey;
          static byte[] alicePubKeyBlob;
          static byte[] bobPubKeyBlob;
  
          static void Main(string[] args)
          {
  
              CreateKeys();
  
              IPAddress ipAddress = IPAddress.Parse("192.168.0.120");
              server = new TcpListener(ipAddress, 54540);
              server.Start();
              var client = server.AcceptTcpClient();
              var stream = client.GetStream();
  
              alicePubKeyBlob = new byte[bobPubKeyBlob.Length];
              stream.Read(alicePubKeyBlob, 0, alicePubKeyBlob.Length);
  
              stream.Write(bobPubKeyBlob, 0, bobPubKeyBlob.Length);
  
              byte[] encrytpedData = new byte[32];
  
              stream.Read(encrytpedData, 0, encrytpedData.Length);
  
              BobReceivesData(encrytpedData);
  
  
          }
  
          private static void CreateKeys()
          {
              //aliceKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256);
              bobKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256);
              //alicePubKeyBlob = aliceKey.Export(CngKeyBlobFormat.EccPublicBlob);
              bobPubKeyBlob = bobKey.Export(CngKeyBlobFormat.EccPublicBlob);
          }
  
  
          private static void BobReceivesData(byte[] encryptedData)
          {
              Console.WriteLine("Bob receives encrypted data");
              byte[] rawData = null;
  
              var aes = new AesCryptoServiceProvider();
  
              int nBytes = aes.BlockSize &gt;&gt; 3;
              byte[] iv = new byte[nBytes];
              for (int i = 0; i &lt; iv.Length; i++)
                  iv[i] = encryptedData[i];
  
              using (var bobAlgorithm = new ECDiffieHellmanCng(bobKey))
              using (CngKey alicePubKey = CngKey.Import(alicePubKeyBlob,
                    CngKeyBlobFormat.EccPublicBlob))
              {
                  byte[] symmKey = bobAlgorithm.DeriveKeyMaterial(alicePubKey);
                  Console.WriteLine("Bob creates this symmetric key with " +
                        "Alices public key information: {0}",
                        Convert.ToBase64String(symmKey));
  
                  aes.Key = symmKey;
                  aes.IV = iv;
  
                  using (ICryptoTransform decryptor = aes.CreateDecryptor())
                  using (MemoryStream ms = new MemoryStream())
                  {
                      var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write);
                      cs.Write(encryptedData, nBytes, encryptedData.Length - nBytes);
                      cs.Close();
  
                      rawData = ms.ToArray();
  
                      Console.WriteLine("Bob decrypts message to: {0}",
                            Encoding.UTF8.GetString(rawData));
                  }
                  aes.Clear();
              }
          }
      }
  }
  </code></pre>
  
  <p>and here is the client code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Net.Sockets;
  using System.Net;
  using System.Security.Cryptography;
  using System.IO;
  
  namespace ClientAlice
  {
      class Program
      {
          static CngKey aliceKey;
          //static CngKey bobKey;
          static byte[] alicePubKeyBlob;
          static byte[] bobPubKeyBlob;
  
          static void Main(string[] args)
          {
  
              CreateKeys();
              bobPubKeyBlob = new byte[alicePubKeyBlob.Length];
  
              TcpClient alice = new TcpClient("192.168.0.120", 54540);
  
              var stream = alice.GetStream();
              stream.Write(alicePubKeyBlob, 0, alicePubKeyBlob.Length);
  
              stream.Read(bobPubKeyBlob, 0, bobPubKeyBlob.Length);
  
  
              byte[] encrytpedData = AliceSendsData(":)");
  
              stream.Write(encrytpedData, 0, encrytpedData.Length);
  
  
          }
  
  
          private static void CreateKeys()
          {
              aliceKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256);
              //bobKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256);
              alicePubKeyBlob = aliceKey.Export(CngKeyBlobFormat.EccPublicBlob);
              //bobPubKeyBlob = bobKey.Export(CngKeyBlobFormat.EccPublicBlob);
          }
  
          private static byte[] AliceSendsData(string message)
          {
              Console.WriteLine("Alice sends message: {0}", message);
              byte[] rawData = Encoding.UTF8.GetBytes(message);
              byte[] encryptedData = null;
  
              using (var aliceAlgorithm = new ECDiffieHellmanCng(aliceKey))
              using (CngKey bobPubKey = CngKey.Import(bobPubKeyBlob,
                    CngKeyBlobFormat.EccPublicBlob))
              {
                  byte[] symmKey = aliceAlgorithm.DeriveKeyMaterial(bobPubKey);
                  Console.WriteLine("Alice creates this symmetric key with " +
                        "Bobs public key information: {0}",
                        Convert.ToBase64String(symmKey));
  
                  using (var aes = new AesCryptoServiceProvider())
                  {
                      aes.Key = symmKey;
                      aes.GenerateIV();
                      using (ICryptoTransform encryptor = aes.CreateEncryptor())
                      using (MemoryStream ms = new MemoryStream())
                      {
                          // create CryptoStream and encrypt data to send
                          var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
  
                          // write initialization vector not encrypted
                          ms.Write(aes.IV, 0, aes.IV.Length);
                          cs.Write(rawData, 0, rawData.Length);
                          cs.Close();
                          encryptedData = ms.ToArray();
                      }
                      aes.Clear();
                  }
              }
              Console.WriteLine("Alice: message is encrypted: {0}",
                    Convert.ToBase64String(encryptedData)); ;
              Console.WriteLine();
              return encryptedData;
          }
      }
  }
  </code></pre>
  
  <p>I thinks it is pretty secure. Every time it sends a different byte array although sending the same info!</p>