Click here to Skip to main content
15,894,896 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 118.3K   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.IO;
namespace VSSProtocol.Utils
{
	/// <summary>
	/// A memory stream that has a read and write position.
	/// </summary>
	public class MemoryStreamRwPosition : MemoryStream
	{
		/// <summary>
		/// Curretn write position.
		/// </summary>
		private long writePosition = 0;
		/// <summary>
		/// Current read position.
		/// </summary>
		private long readPosition = 0;

		///<summary>
		///Writes a block of bytes to the current stream using data read from buffer.
		///</summary>
		///
		///<param name="offset">The byte offset in buffer at which to begin writing from. </param>
		///<param name="count">The maximum number of bytes to write. </param>
		///<param name="buffer">The buffer to write data from. </param>
		///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
		///<exception cref="T:System.ArgumentNullException">buffer is null. </exception>
		///<exception cref="T:System.NotSupportedException">The stream does not support writing. For additional information see <see cref="P:System.IO.Stream.CanWrite"></see>.-or- The current position is closer than count bytes to the end of the stream, and the capacity cannot be modified. </exception>
		///<exception cref="T:System.ArgumentOutOfRangeException">offset or count are negative. </exception>
		///<exception cref="T:System.ObjectDisposedException">The current stream instance is closed. </exception>
		///<exception cref="T:System.ArgumentException">offset subtracted from the buffer length is less than count. </exception><filterpriority>2</filterpriority>
		public override void Write(byte[] buffer, int offset, int count)
		{
			Position = writePosition;
			base.Write(buffer, offset, count);
			writePosition = Position;
		}

		///<summary>
		///Writes a byte to the current stream at the current position.
		///</summary>
		///
		///<param name="value">The byte to write. </param>
		///<exception cref="T:System.NotSupportedException">The stream does not support writing. For additional information see <see cref="P:System.IO.Stream.CanWrite"></see>.-or- The current position is at the end of the stream, and the capacity cannot be modified. </exception>
		///<exception cref="T:System.ObjectDisposedException">The current stream is closed. </exception><filterpriority>2</filterpriority>
		public override void WriteByte(byte value)
		{
			byte[] byteValue = new byte[1];
			byteValue[0] = value;
			Write(byteValue, 0, 1);
		}

		///<summary>
		///Reads a byte from the current stream.
		///</summary>
		///
		///<returns>
		///The byte cast to a <see cref="T:System.Int32"></see>, or -1 if the end of the stream has been reached.
		///</returns>
		///
		///<exception cref="T:System.ObjectDisposedException">The current stream instance is closed. </exception><filterpriority>2</filterpriority>
		public override int ReadByte()
		{
			byte[] byteValue = new byte[1];
			Read(byteValue, 0, 1);
			return byteValue[0];
		}

		///<summary>
		///Reads a block of bytes from the current stream and writes the data to buffer.
		///</summary>
		///
		///<returns>
		///The total number of bytes written into the buffer. This can be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached before any bytes are read.
		///</returns>
		///
		///<param name="offset">The byte offset in buffer at which to begin reading. </param>
		///<param name="count">The maximum number of bytes to read. </param>
		///<param name="buffer">When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the characters read from the current stream. </param>
		///<exception cref="T:System.ArgumentNullException">buffer is null. </exception>
		///<exception cref="T:System.ObjectDisposedException">The current stream instance is closed. </exception>
		///<exception cref="T:System.ArgumentException">offset subtracted from the buffer length is less than count. </exception>
		///<exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception><filterpriority>2</filterpriority>
		public override int Read(byte[] buffer, int offset, int count)
		{
			Position = readPosition;
			int readBytes = base.Read(buffer, offset, count);
			readPosition = Position;
			return readBytes;
		}


		/// <summary>
		/// Curretn write position.
		/// </summary>
		public long WritePosition
		{
			get { return writePosition; }
			set { writePosition = value; }
		}

		/// <summary>
		/// Current read position.
		/// </summary>
		public long ReadPosition
		{
			get { return readPosition; }
			set { readPosition = value; }
		}


		///<summary>
		///Gets or sets the current position within the stream. Both read and write position are set to the same number.
		///</summary>
		///
		///<returns>
		///The current position within the stream.
		///</returns>
		///
		///<exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
		///<exception cref="T:System.ArgumentOutOfRangeException">The position is set to a negative value or a value greater than <see cref="F:System.Int32.MaxValue"></see>. </exception><filterpriority>2</filterpriority>
		public override long Position
		{
			get { return base.Position; }
			set
			{
				base.Position = value;
				writePosition = value;
				readPosition = value;
			}
		}
	}
}

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