Baran Topal

Baran Topal


May 2024
M T W T F S S
« Feb    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories


CryptoServices in .NET: Hosting the service and Client

baranbaran

Hosting the service and Client

This is the .NET Realm implementation. Note there are 4 other parts to fulfill this cryptoservices completely.

Previously, we created our WCF service which can be run via built-in client service in VS2010 but in real life, the web services are running in a seperate realm. This can be under a web server, like IIS or WAS in 2008, or you can create a windows service which is plain easy.

Here, I will be doing none of those for simplicity, I will create a host console application which hosts the WCF service and listens the given port. Note that, the port can be anything but be sure, it’s not colliding with another daemon’s port.

The service is running first and the client, “then”, requests something.

By default, VS2010 lets you run only one startup project, here in our case, we need to have 2 startup projects (alternatively, you can run 2 VS2010 instances and fulfill this).

Right click on Solution and follow as this shot:

First, let me have the host console:


/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Company: -                                                            *
* Assignment: Cyclic Redundancy Check + 3DES + EF + WCF                 *
* Programmer: Baran Topal                                               *
* Solution: Crypto                                                      *
* Project Name: ConsoleHost                                             *
* File name: Program.cs                                                 *
*                                                                       *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*                                                                                           *
* LICENSE: This source file is subject to have the protection of GNU General                *
* Public License. You can distribute the code freely but storing this license information.  *
* Contact Baran Topal if you have any questions. barantopal@barantopal.com                  *
*                                                                                           *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel;
using CryptoServiceLibrary;
using System.Threading;
// Host that keeps the web service, client connects here
namespace ConsoleHost
{
    class Program
    {
        static void Main(string[] args)
        {
            string address = "http://localhost:8000/EncDecService";
            using (var host = new ServiceHost(typeof(EncDecService)))
            {
                ServiceEndpoint endPoint = host.AddServiceEndpoint(typeof(IEncDecService), new BasicHttpBinding(), address);
                host.Open();
                foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
                {
                    Console.WriteLine("{0} ({1})",
                    endpoint.Address.ToString(), endpoint.Binding.Name);
                }
                Console.WriteLine();
                Console.WriteLine("Press any key to stop the service.");
                Console.ReadKey();
                //Service stopped
            }
        }
    }
}

Note that this console will run infinitely until a key is pressed on console (No need an infinite loop).


Now, the client can run but let the client have a sleep for 5 seconds so that it won’t fall in an exception due to no binding point.


/* 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Company: -								 			 *            
 * Assignment: Cyclic Redundancy Check + 3DES + EF + WCF	                                 *
 * Deadline: -                           	 						 *
 * Programmer: Baran Topal                   							 *
 * Solution: Crypto					 					 *
 * Project Name: Client          	        	 					 *
 * File name: Program.cs                       							 *
 *                                           							 *      
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *	                                                                                         *
 *  LICENSE: This source file is subject to have the protection of GNU General                   *
 *	Public License. You can distribute the code freely but storing this license information. *
 *	Contact Baran Topal if you have any questions. barantopal@barantopal.com                 *
 *	                                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.IO;
using System.Threading;
using CryptoServiceLibrary;

// Web service client
namespace Client
{
    class Program
    {
        private static IEncDecService httpChannel = null;
        static void Main(string[] args)
        {
            // Prevent possible exception by giving a delay
            Thread.Sleep(5000);
            var httpEndPoint = new EndpointAddress("http://localhost:8000/EncDecService");

            // You can add a service reference and just use a proxy
            // mex in config is a relative address, so you have to add a base address in config
            // service behavior for publishing metadata

            httpChannel = ChannelFactory.CreateChannel(new BasicHttpBinding(), httpEndPoint);

            // Web service client sends the data and retrieves the result
            bool enc = httpChannel.Encrpyt("loremipsum_inp.txt", "loremipsum_enc.enc", "1234-4567-8910-2345");
            bool dec = httpChannel.Decrypt("loremipsum_enc.enc", "loremipsum_dec.dec", "1234-4567-8910-2345");

            if (enc && dec)
                if (enc)
                    Console.WriteLine("Encryption and decryption successful!");
                else
                    Console.WriteLine("Hopefully you are not seeing this...");
        }
    }
}


In the end, you will have the following:

You can interpret this as follows:
1) Server runs.
2) Client invokes encrpytion and decryption.
3) Server handles the request, calculate the hash for both plain and encrypted files, encrypts the file content, stores both files as binary format and informs back the client.
You may argue about storing a file content as a binary but this is just a sample project. In real life, you should go for storing a file in an actual path or utilize CDN. One counter-argument here in this case is that, the stored files are actually small and it’s using/exploiting the security features of the underlying database server.