Click here to Skip to main content
15,867,568 members
Articles / Mobile Apps / Windows Phone 7

Windows Phone 7: How to Encrypt Communication with Desktop Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
24 Apr 2011CPOL2 min read 24.4K   126   8   15
A simple example showing how to implement encrypted communication between Windows Phone 7 and standalone .NET application.

Summary

This is a simple example showing how to implement encrypted communication between Windows Phone 7 and standalone .NET application.

Introduction

The example below 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.
(The framework is free and 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.

C#
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.

C#
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:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Slovakia Slovakia
My programming path started in 1987 when I got my first computer Sharp MZ-800.
It came with 8 bit CPU Z80, 64Kb RAM and the tape recorder. It was a great machine. I think I still have it somewhere.
I was fascinated and I started to write first programs. Eventually I became developer and software architect. I like innovations and clean nice solutions.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Marcelo Ricardo de Oliveira29-Apr-11 16:34
mvaMarcelo Ricardo de Oliveira29-Apr-11 16:34 
GeneralMy vote of 5 Pin
Member 432084429-Apr-11 7:11
Member 432084429-Apr-11 7:11 
GeneralDownload problem Pin
Jeff Law26-Apr-11 20:10
Jeff Law26-Apr-11 20:10 
GeneralRe: Download problem Pin
Jeff Law26-Apr-11 22:35
Jeff Law26-Apr-11 22:35 
GeneralRe: Download problem Pin
Ondrej_Uzovic26-Apr-11 22:45
Ondrej_Uzovic26-Apr-11 22:45 
GeneralRe: Download problem Pin
Cracklin Hot29-Apr-11 4:10
Cracklin Hot29-Apr-11 4:10 
GeneralRe: Download problem Pin
Jeff Law29-Apr-11 12:37
Jeff Law29-Apr-11 12:37 
GeneralMy vote of 5 Pin
adigtm424-Apr-11 22:29
adigtm424-Apr-11 22:29 
Generalhelp... Pin
adigtm424-Apr-11 9:01
adigtm424-Apr-11 9:01 
GeneralRe: help... Pin
Ondrej_Uzovic24-Apr-11 11:21
Ondrej_Uzovic24-Apr-11 11:21 
GeneralRe: help... Pin
adigtm424-Apr-11 22:24
adigtm424-Apr-11 22:24 
GeneralRe: help... Pin
adigtm425-Apr-11 1:43
adigtm425-Apr-11 1:43 
GeneralRe: help... Pin
Ondrej_Uzovic25-Apr-11 8:39
Ondrej_Uzovic25-Apr-11 8:39 
GeneralRe: help... Pin
adigtm426-Apr-11 4:03
adigtm426-Apr-11 4:03 
GeneralRe: help... Pin
Ondrej_Uzovic26-Apr-11 9:21
Ondrej_Uzovic26-Apr-11 9:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.