<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn's C# Programming Blog &#187; Learn C#</title>
	<atom:link href="http://www.dijksterhuis.org/tag/learn-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Measuring memory usage of object creation in C#</title>
		<link>http://www.dijksterhuis.org/measuring-memory-usage-object-creation/</link>
		<comments>http://www.dijksterhuis.org/measuring-memory-usage-object-creation/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 05:49:03 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[garbage collector]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[memory usage]]></category>
		<category><![CDATA[object creation]]></category>
		<category><![CDATA[process]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=467</guid>
		<description><![CDATA[As your application grows it can be useful to get an idea of how much memory a particular data structure is using in memory. Measuring memory in a garbage collected environment is a somewhat of a moving target as the Garbage collector is able to move things around in the background.The .NET libraries offer two [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><em>As your application grows it can be useful to get an idea of how much memory a particular data structure is using in memory. Measuring memory in a garbage collected environment is a somewhat of a moving target as the Garbage collector is able to move things around in the background.The .NET libraries offer two ways of measuring your applications memory. The total amount of memory allocated to the process (which includes code, unique libraries) and the amount of memory used that the Garbage collector is aware off (which is only your applications variables). This second set is of course a subset of the total amount of memory. </em></p>
<p><span id="more-467"></span></p>
<p><strong>Measuring your process memory usage</strong></p>
<p>The .NET API allows you to retrieve the total amount of memory allocated to a process through the <em>System.Diagnostics.Process</em> class. This variable is useful is you are interested in the total memory footprint of your process, but it is too coarse to measure how much memory a single object takes.</p>
<pre class="brush: c#">
long Process_MemoryStart = 0;
Process MyProcess = System.Diagnostics.Process.GetCurrentProcess();
Process_MemoryStart = MyProcess.PrivateMemorySize64;
</pre>
<p>This will return the total amount of memory uniquely allocated to your process in bytes (not shared with other processes).This measure of memory includes everything from code, libraries, the execution environment, stack, variables etc. If you try to measure the creation of a new object in memory using this variable it is likely to show no change.  It is very likely that the difference shown is zero as some memory for new objects is pre-allocated.</p>
<p><strong>Using the garbage collector to gain insights into memory allocation</strong></p>
<p>If you are building a multi threaded application the following code won&#8217;t work as expected, other threads might be too busy creating new objects and destroying to gain a clear insight into how much memory an object uses.We make use of the fact that the Garbage collector can report how much allocated memory it is aware off. As the Garbage collector manages just the objects we create for our application &#8212; we can use it to measure an objects size.</p>
<p>By calling <em>System.GC.GetTotalMemory()</em> we can obtain a measure of how much memory the Garbage collector is managing. If we call this function again after we allocated our object, we can calculate the difference.</p>
<pre class="brush: c#">
// Measure starting point memory use
GC_MemoryStart = System.GC.GetTotalMemory(true);

// Allocate a new byte array of 20000 elements (about 20000 bytes)
MyByteArray = new byte[20000];

// Obtain measurements after creating the new byte[]
GC_MemoryEnd = System.GC.GetTotalMemory(true);

// Ensure that the Array stays in memory and doesn&#039;t get optimized away
MyByteArray[1] = 20;
</pre>
<p>By calling System.GC.GetTotalMemory with &#8220;true&#8221; as a parameter we ensure that the Garbage collector makes at least an efford to clean up before reporting the amount of memory under its management.<em> </em>To avoid any compiler optimizations that would shift code around, we need to make sure that our newly created byte array is accessed again after we make the second GetTotalMemory() call.</p>
<p>The code listed at the end of this post reports something similar to the following when run:</p>
<pre class="brush: c#">
Memory Use reported by GC: 20480
Memory Use reported by Process: 65536
Memory allocated by the Garbage collector: 135168 bytes
Memory allocated by the process: 2641920 bytes
</pre>
<p>As you can see, allocating a 20,000 element byte array takes up about 20Kb (with some additional overhead). Apparently the garbage collector decided to increase the applications total memory by 64Kb as the total process size increased by that much.You mileage may vary depending on your run. The final two figures show how much memory is under management by the Garbage collector for our simple application (about 135Kb) and the how much the Operating system needed to allocate it the whole application (2 Megabyte).</p>
<pre class="brush: c#">
using System;
using System.Diagnostics;

namespace ProfileTest
{
	class MainClass
	{
		public static void Main(string[] args)
		{
	      long GC_MemoryStart = 0;
    	  long GC_MemoryEnd = 0;
		  long Process_MemoryStart = 0;
		  long Process_MemoryEnd = 0;
		  byte[] MyByteArray;

		  Process MyProcess = System.Diagnostics.Process.GetCurrentProcess();

		  // Measure starting point memory use
      	  GC_MemoryStart = System.GC.GetTotalMemory(true);
		  Process_MemoryStart = MyProcess.PrivateMemorySize64;

		  // Allocate a new byte array of 20000 elements (about 20000 bytes)
		  MyByteArray = new byte[20000];

		  // Obtain measurements after creating the new byte[]
		  GC_MemoryEnd = System.GC.GetTotalMemory(true);
		  Process_MemoryEnd = MyProcess.PrivateMemorySize64;

		  // Ensure that the Array stays in memory and doesn&#039;t get optimized away
		  MyByteArray[1] = 20;

		  // Show the difference after allocating the memory
		  Console.WriteLine(&quot;Memory Use reported by GC: {0}&quot; , GC_MemoryEnd - GC_MemoryStart );
		  Console.WriteLine(&quot;Memory Use reported by Process: {0}&quot; , Process_MemoryEnd - Process_MemoryStart );

		  // Show how much memory was allocated by the process, and how much by the GC
		  Console.WriteLine(&quot;Memory allocated by the Garbage collector: {0} bytes&quot;, GC_MemoryEnd);
		  Console.WriteLine(&quot;Memory allocated by the process: {0} bytes&quot;, Process_MemoryEnd);
		}
	}
}
</pre>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/measuring-memory-usage-object-creation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Encrypting and Decrypting a C# string</title>
		<link>http://www.dijksterhuis.org/encrypting-decrypting-string/</link>
		<comments>http://www.dijksterhuis.org/encrypting-decrypting-string/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 06:31:27 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[decryption]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[md5]]></category>
		<category><![CDATA[strings]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=453</guid>
		<description><![CDATA[The .NET C# library provides all the basic elements for encrypting a string with a passphrase and decrypting it later. Doing this however requires a few steps in between. This post show a simple set of routines to help you do just that. We use the TripleDES encryption suite to do the actual encryption, with [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><em>The .NET C# library provides all the basic elements for encrypting a string with a passphrase and decrypting it later. Doing this however requires a few steps in between. This post show a simple set of routines to help you do just that. We use the TripleDES encryption suite to do the actual encryption, with a little help from the MD5 hash sum generator.<br />
</em></p>
<p>The complete source code is listed below, but lets have a little look at how it works first.</p>
<p><span id="more-453"></span></p>
<p><strong>The problem</strong></p>
<p>I want to take a string, and then encrypt it using a password. The result should be a Base64 encoded string that I can store somewhere relatively safe.</p>
<pre class="brush: c#">
            // The message to encrypt.
            string Msg = &quot;This world is round, not flat, don&#039;t believe them!&quot;;
            string Password = &quot;secret&quot;;

            string EncryptedString = EncryptString(Msg, Password);
            string DecryptedString = DecryptString(EncryptedString, Password);

            Console.WriteLine(&quot;Message: {0}&quot;,Msg);
            Console.WriteLine(&quot;Password: {0}&quot;,Password);
            Console.WriteLine(&quot;Encrypted string: {0}&quot;,EncryptedString);
            Console.WriteLine(&quot;Decrypted string: {0}&quot;,DecryptedString);
</pre>
<p>In the EncryptString function we apply the TripleDES algorithm with a 128 bit key. But first we need to turn the above passphrase (&#8217;secret&#8217;) into a 128 bit key.  One useful coincidence is that the MD5 hash algorithm accepts a set of bytes of any length and turns them into a 128 bit hash. So by running the password through the MD5 hashing algorithm we create our key.</p>
<pre class="brush: c#">
            // Step 1. We hash the passphrase using MD5
            // We use the MD5 hash generator as the result is a 128 bit byte array
            // which is a valid length for the TripleDES encoder we use below

            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
</pre>
<p>The TripleDES algorithm itself turns a byte array into an encrypted  byte array. So we first need to convert our C# message string (which is Unicode encoded) into a byte array  through the System.Text.UTF8Encoding encoder.</p>
<p>The key is used to initialize the TripleDES algorithm. In addition we need to specify that we will only encode something once (CipherMode.ECB) and because its unlikely that our source string fits into a single TripleDES block we need to specify how we want to pad any remaining bytes (PaddingMode.PKCS7).</p>
<pre class="brush: c#">
            // Step 2. Create a new TripleDESCryptoServiceProvider object
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

            // Step 3. Setup the encoder
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
</pre>
<p>The encrypted byte array is finally converted into a Base64 encoded string for easy storage. The <em>DecryptString</em> function is very similar to the encryption function, except that it turns the Base64 encoded encrypted message back into the original UTF8 string.</p>
<p><strong>Drawbacks to the above method</strong></p>
<p>To keep the code above straightforward we made use of the fact that an MD5 hash is exactly 128 bits in length. The C# TripleDES code accepts three possible key lengths: 64 bit, 128 bit and 192 bit. Only 192 bit keys are truly TripleDES, the 128 bit key length we obtain from the MD5 hash is only sufficient for Double DES. According to Wikipedia, that would make its real key strength only equivalent to 80 bits.</p>
<p><strong>The Source code</strong></p>
<pre class="brush: c#">
using System;
using System.Text;
using System.Security.Cryptography;

namespace EncryptStringSample
{
    class MainClass
    {

        public static string EncryptString(string Message, string Passphrase)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

            // Step 1. We hash the passphrase using MD5
            // We use the MD5 hash generator as the result is a 128 bit byte array
            // which is a valid length for the TripleDES encoder we use below

            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

            // Step 2. Create a new TripleDESCryptoServiceProvider object
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

            // Step 3. Setup the encoder
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;

            // Step 4. Convert the input string to a byte[]
            byte[] DataToEncrypt = UTF8.GetBytes(Message);

            // Step 5. Attempt to encrypt the string
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }

            // Step 6. Return the encrypted string as a base64 encoded string
            return Convert.ToBase64String(Results);
        }

        public static string DecryptString(string Message, string Passphrase)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

            // Step 1. We hash the passphrase using MD5
            // We use the MD5 hash generator as the result is a 128 bit byte array
            // which is a valid length for the TripleDES encoder we use below

            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

            // Step 2. Create a new TripleDESCryptoServiceProvider object
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

            // Step 3. Setup the decoder
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;

            // Step 4. Convert the input string to a byte[]
            byte[] DataToDecrypt = Convert.FromBase64String(Message);

            // Step 5. Attempt to decrypt the string
            try
            {
                ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            }
            finally
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }

            // Step 6. Return the decrypted string in UTF8 format
            return UTF8.GetString( Results );
        }

        public static void Main(string[] args)
        {
            // The message to encrypt.
            string Msg = &quot;This world is round, not flat, don&#039;t believe them!&quot;;
            string Password = &quot;secret&quot;;

            string EncryptedString = EncryptString(Msg, Password);
            string DecryptedString = DecryptString(EncryptedString, Password);

            Console.WriteLine(&quot;Message: {0}&quot;,Msg);
            Console.WriteLine(&quot;Password: {0}&quot;,Password);
            Console.WriteLine(&quot;Encrypted string: {0}&quot;,EncryptedString);
            Console.WriteLine(&quot;Decrypted string: {0}&quot;,DecryptedString);
        }
    }
}
</pre>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/encrypting-decrypting-string/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
