Search This Blog

Thursday, April 21, 2011

Windows Phone 7: How to Encrypt Communication with Desktop Application

Summary: Simple example showing how to implement encrypted communication between Windows Phone 7 and standalone .NET application.


The example bellow implements a simple .NET application as a service and a Windows Phone 7 application as a client. The service counts the number of words in the given text. The client then uses the service to get amount of words in the text and displays the result.

The communication between the client and the service is protected by AES (Advanced Encryption Standard).

Also, since the network connection does not have to be stable (e.g. weak signal), the example uses the buffered messaging. The buffered messaging detects disconnections, automatically tries to reconnect and meanwhile stores sent messages in the buffer. Then, when the connection is available again, the messages stored in the buffer are sent to the receiver.

The example is based on the Eneter Messaging Framework 2.0.
(Full, not limited and for non-commercial usage free version of the framework can be downloaded from http://www.eneter.net. The online help for developers can be found at http://www.eneter.net/OnlineHelp/EneterMessagingFramework/Index.html.)

Service Application
The service is a standalone .NET application and is responsible for the receiving of messages and counting words in the given text.
Please notice, to run the HTTP listening application you must execute it under sufficient user rights. Otherwise the application will run, but will not listen. (The framework logs the Access denied error on the debug port.)
The whole implementation is very simple.

using System;
using System.Text.RegularExpressions;
using Eneter.Messaging.DataProcessing.Serializing;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.Composites;
using Eneter.Messaging.MessagingSystems.HttpMessagingSystem;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;

namespace EncryptedService
{
    class Program
    {
        // Receiver receiving a text message and returning back the number
        // of words in the text message.
        private static IDuplexTypedMessageReceiver<int, string> myReceiver;

        static void Main(string[] args)
        {
            // Create Http messaging.
            IMessagingSystemFactory anUnderlyingMessaging = new HttpMessagingSystemFactory();

            // The network can be unstable. Therefore, let's use the buffered messaging with
            // the monitor checking the connection.
            // If the client does not ping longer than 5 seconds, the client is considered to be disconnected
            // and its response messages are stored in the buffer.
            // If the connection occurs within 15 seconds, the response messages are sent to the client.
            IMessagingSystemFactory aBufferedMessaging = new BufferedMonitoredMessagingFactory(
                anUnderlyingMessaging,
                new XmlStringSerializer(),
                TimeSpan.FromMilliseconds(15000),   // maximum time, the client can be offline
                TimeSpan.FromMilliseconds(500),     // 'ping' frequency - not applicable for the service
                TimeSpan.FromMilliseconds(5000)     // maximum time, the 'ping' message does not have to be received
                );


            IDuplexInputChannel anInputChannel = aBufferedMessaging.CreateDuplexInputChannel("http://127.0.0.1:8035/MyService/");

            // Helper: If you are interested to see the encrypted message.
            anInputChannel.MessageReceived += OnEncryptedMessageReceived;

            // Create serializer using the AES encryption.
            // Note: The serializer is used to deserialize incoming messages
            //       and serialize sent response messages.
            AesSerializer aSerializer = new AesSerializer("My secret password");

            // Create message receiver - response sender.
            IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory(aSerializer);
            myReceiver = aSenderFactory.CreateDuplexTypedMessageReceiver<int, string>();
            myReceiver.MessageReceived += OnMessageReceived;

            Console.WriteLine("The service is running.");
            Console.WriteLine("Press enter to stop ...");

            // Attach the duplex input channel and start listening.
            myReceiver.AttachDuplexInputChannel(anInputChannel);

            Console.ReadLine();

            myReceiver.DetachDuplexInputChannel();
        }

        private static void OnMessageReceived(object sender, TypedRequestReceivedEventArgs<string> e)
        {
            if (e.ReceivingError == null)
            {
                // Display the incoming text message.
                Console.WriteLine("Received message: {0}\n", e.RequestMessage);

                // Calculate number of words in the string.
                MatchCollection aWords = Regex.Matches(e.RequestMessage, @"[\S]+");
                int aNumberOfWords = aWords.Count;

                // Send back the number of words.
                myReceiver.SendResponseMessage(e.ResponseReceiverId, aNumberOfWords);
            }
        }

        // This is just a helper method to display the encrypted message.
        private static void OnEncryptedMessageReceived(object sender, DuplexChannelMessageEventArgs e)
        {
            string anEncryptedMessage = "";
            byte[] anEncryptedArray = e.Message as byte[];
            foreach (byte aByte in anEncryptedArray)
            {
                anEncryptedMessage += aByte.ToString("x2");
            }

            Console.WriteLine("Encrypted Message: {0}", anEncryptedMessage);
        }

    }
}


Client Application
The client is a Windows Phone 7 application and is responsible for receiving text from the user and using the service to get the number of words in the given text. Then it displays the result.
(Notice, if you run the application from the Visual Studio, do not forget to set, that the application shall be deployed on 'Windows Phone 7 Emulator'.)
The client application uses the assembly built for the Windows Phone 7,  Eneter.Messaging.Framework.Phone.dll.

using System;
using System.Windows;
using Eneter.Messaging.DataProcessing.Serializing;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.Composites;
using Eneter.Messaging.MessagingSystems.HttpMessagingSystem;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Microsoft.Phone.Controls;

namespace EncryptedClientWP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Sender sending text messages and receiving int response messages.
        private IDuplexTypedMessageSender<int, string> mySender;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            OpenConnection();
        }

        private void OpenConnection()
        {
            // Create Http messaging.
            // Note: The default constructor routes received response
            //       messages into the Silverlight thread.
            //       If it is not desired, then it can be changed.
            IMessagingSystemFactory anUnderlyingMessaging = new HttpMessagingSystemFactory();

            // The cell-phone network can be unstable.
            // Therefore, let's use the buffered messaging and
            // the monitor checking if the connection is still open.
            // Create buffered messaging, that will be able to work offline 1 minute.
            // During the offline time, the sent messages are stored in the buffer
            // and the framework tries to reconnect.
            IMessagingSystemFactory aBufferedMessaging = new BufferedMonitoredMessagingFactory(
                anUnderlyingMessaging,
                new XmlStringSerializer(),
                TimeSpan.FromMinutes(1),            // maximum offline time
                TimeSpan.FromMilliseconds(500),     // how often the 'ping' checking the connection is invoked
                TimeSpan.FromMilliseconds(1000)     // maximum time, the response for the 'ping'
                                                    // shall be received
                );
            IDuplexOutputChannel anOutputChannel = 
                aBufferedMessaging.CreateDuplexOutputChannel("http://127.0.0.1:8035/MyService/");

            // Create serializer using the AES encryption.
            // Note: The serializer is used to serialize sent messages and
            //       deserialize incoming response messages.
            //       The password must be same as on the service side.
            AesSerializer aSerializer = new AesSerializer("My secret password");

            // Create message sender - response receiver.
            IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory(aSerializer);
            mySender = aSenderFactory.CreateDuplexTypedMessageSender<int, string>();
            mySender.ResponseReceived += OnResponseReceived;

            // Attach duplex output channel and be able to send
            // messages and receive response messages.
            mySender.AttachDuplexOutputChannel(anOutputChannel);
        }

        private void PhoneApplicationPage_Unloaded(object sender, RoutedEventArgs e)
        {
            // Disconnect from the service.
            mySender.DetachDuplexOutputChannel();
        }

        private void SendButton_Click(object sender, RoutedEventArgs e)
        {
            // Send the message.
            // Note: The message will be serialized and encrypted by AES and
            //       then sent to the service.
            mySender.SendRequestMessage(TextMessage.Text);
        }

        private void OnResponseReceived(object sender, TypedResponseReceivedEventArgs<int> e)
        {
            if (e.ReceivingError == null)
            {
                // The message is received in the Silverlight thread,
                // so we do not have to route it to the Silverlight thread.
                // Display the number of words.
                Result.Text = e.ResponseMessage.ToString();
            }
        }
    }
}


And the communicating applications are displayed here.

No comments:

Post a Comment