title: Digital signature in C#
body: <p>So I have a server and a clients.
  And I have a task/problem with security.
  Conversation between server and client:
  Client : Give me config file, Server!
  Server : Sending config file with digital signature
  Client : Receive data</p>
  
  <p>So, What approach should I choose?</p>
  
  <ol>
  <li>Generate private and public keys(server side). </li>
  <li>Save private key in the xml-file (TIP1)</li>
  <li>Clients will be spread with public key(which was generate together with private key)</li>
  </ol>
  
  <p>than, when clients will send request to server :
  1. Server get private key from xml-file(TIP1) and send config file + digital signature(encrypt by private key)
  2. Client receive response from server and verify digital signature by public key</p>
  
  <p>How can I implement this idea in C# ?
  Keys(private and public) I'll be generate by special program. So private key I'll be save in xml-file.</p>
  
  <p>ADDITIONALLY</p>
  
  <p>I wrote some class that maybe do work that I want :</p>
  
  <pre><code>public class Cryptography
  {
      private const string CONTAINER_NAME = "MyContainer";
  
      private static RSACryptoServiceProvider rsa;
      private static MD5 md5;
  
      private static void AssignParameter()
      {           
          CspParameters cspParams;
          cspParams = new CspParameters();
          cspParams.KeyContainerName = CONTAINER_NAME;
          cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
  
          rsa = new RSACryptoServiceProvider(cspParams);
      }
  
      static Cryptography()
      {
          md5 = MD5.Create();
      }
  
      public static byte[] CreateSignature(byte[] data, string pathToKey)
      {
          AssignParameter();
  
          StreamReader reader = new StreamReader(pathToKey);
          string privateKeyXML = reader.ReadToEnd();
          rsa.FromXmlString(privateKeyXML);
          reader.Close();
  
          RSAPKCS1SignatureFormatter RSAform = new RSAPKCS1SignatureFormatter(rsa);
          RSAform.SetHashAlgorithm("MD5");
  
          byte[] hashData = md5.ComputeHash(data);
  
          return RSAform.CreateSignature(hashData);
      }
  
      public static bool VerifySignature(byte[] originalData, byte[] data, string pathToKey)
      {
          StreamReader reader = new StreamReader(pathToKey);
          string publicKeyXML = reader.ReadToEnd();
          rsa.FromXmlString(publicKeyXML);
          reader.Close();
  
          RSAPKCS1SignatureDeformatter RSAdeform = new RSAPKCS1SignatureDeformatter(rsa);
          RSAdeform.SetHashAlgorithm("MD5");
  
          byte[] hashOriginalData = md5.ComputeHash(originalData);
  
          return RSAdeform.VerifySignature(hashOriginalData, data);
      }
  
      public static void AssignNewKey()
      {
          AssignParameter();
  
          StreamWriter writer = new StreamWriter(@"D:\cryptography\privatekey.xml");
          string privatePrivateKeyXML = rsa.ToXmlString(true);
          writer.Write(privatePrivateKeyXML);
          writer.Close();
  
          writer = new StreamWriter(@"D:\cryptography\publickey.xml");
          string publicOnlyKeyXML = rsa.ToXmlString(false);
          writer.Write(publicOnlyKeyXML);
          writer.Close();
  
      }
  
  }
  </code></pre>
  
  <p>What do you think ?</p>