Click here to Skip to main content
15,887,746 members
Articles / Programming Languages / C#

Creating a secure channel

Rate me:
Please Sign up or sign in to vote.
4.90/5 (33 votes)
24 May 2008CDDL13 min read 117.9K   2.9K   86  
The purpose of this article is to explain how a secure channel is built. The article will explain the structure of a Very Simple Secured Protocol (VSSP) that sits above the TCP/IP layer.
using System.Text;
using VSSProtocol.DataLayer;
using VSSProtocol.Session;
using System;

namespace VSSProtocol.Messages
{
	/// <summary>
	/// Master key message contains an encryption of the master key. This message uses the session values that where
	/// defined during the ClientHello messages. Pay attention to the fact that both constructors do the opposite work...one
	/// encypted (client) and one decryptes (server).
	/// </summary>
	internal class ClientKeyExchangeMessage : DataPacket
	{
		/// <summary>
		/// The premaster key that was chosen. Decrypted.
		/// </summary>
		private readonly String preMasterKey;
		/// <summary>
		/// Create a new ClientKeyExchange message.
		/// </summary>
		/// <param name="session">Session to use in order to retreive the public encryption algorithm.</param>
		/// <param name="preMasterKey">From this string all keys will be derived.</param>
		public ClientKeyExchangeMessage(ISession session, String preMasterKey)
			: base(PacketTypeEnum.ClientKeyExchange)
		{
			if (preMasterKey == null || session == null)
			{
				throw new ArgumentNullException();
			}
			this.preMasterKey = preMasterKey;
			// Prepare data byte array.
			byte[] preMasterByte = Encoding.ASCII.GetBytes(preMasterKey);
			// We can compress now the premaster key (compressing at the data layer will be useless) but I'll skip it.
			setData(session.AsymmetricAlgorithm.Encrypt(preMasterByte));
		}

		/// <summary>
		/// Create a new ClientKeyExchange message. The message contains the encrypted values of the premaster key.
		/// </summary>
		/// <param name="session">Session to use in order to retreive the public encryption algorithm.</param>
		/// <param name="message">Message to convert.</param>
		public ClientKeyExchangeMessage(ISession session, byte[] message)
			: base(PacketTypeEnum.ClientKeyExchange)
		{
			setData(message);
			// First decrypt.
			byte[] decryptedPreMaster = session.AsymmetricAlgorithm.Decrypt(message);
			// Get string value.
			preMasterKey = Encoding.ASCII.GetString(decryptedPreMaster);
		}


		/// <summary>
		/// The premaster key that was chosen. Decrypted.
		/// </summary>
		public string PreMasterKey
		{
			get { return preMasterKey; }
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Software Developer
Israel Israel
A computer science master student at Bar Ilan University under the supervision of Dr. Gal Kaminka.
Dealing mainly with trajectory mining.

Comments and Discussions