<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>RranjanK&#039;s Blogs &#187; CodeProject</title>
	<atom:link href="http://raviranjankr.wordpress.com/category/codeproject/feed/" rel="self" type="application/rss+xml" />
	<link>http://raviranjankr.wordpress.com</link>
	<description>Life between Code and Coffee</description>
	<lastBuildDate>Fri, 17 May 2013 14:22:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='raviranjankr.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/4f5ebd2707f6e2d0701ea4c4d0555a61?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>RranjanK&#039;s Blogs &#187; CodeProject</title>
		<link>http://raviranjankr.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://raviranjankr.wordpress.com/osd.xml" title="RranjanK&#039;s Blogs" />
	<atom:link rel='hub' href='http://raviranjankr.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Cryptography : A Basic Introduction of Hashing and HashAlgo class for beginners – Part 2</title>
		<link>http://raviranjankr.wordpress.com/2012/10/25/cryptography-a-basic-introduction-of-hashing-and-hashalgo-class-for-beginners-part-2/</link>
		<comments>http://raviranjankr.wordpress.com/2012/10/25/cryptography-a-basic-introduction-of-hashing-and-hashalgo-class-for-beginners-part-2/#comments</comments>
		<pubDate>Thu, 25 Oct 2012 15:54:18 +0000</pubDate>
		<dc:creator>Ravi Ranjan Kumar</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Cryptography]]></category>

		<guid isPermaLink="false">http://raviranjankr.wordpress.com/?p=847</guid>
		<description><![CDATA[In the previous part of this blog we have learned about basic concept of Hashing with its practical example, we&#8217;ve also read about confusing question for beginner which Is both hashing and Encryption are Same ? then after we covered &#8230; <a href="http://raviranjankr.wordpress.com/2012/10/25/cryptography-a-basic-introduction-of-hashing-and-hashalgo-class-for-beginners-part-2/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=847&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In the previous part of this blog we have learned about basic concept of Hashing with its practical example, we&#8217;ve also read about confusing question for beginner which Is both hashing and Encryption are Same ? then after we covered about algorithm classes of Hashing.</p>
<h3><strong><span style="color:#800000;">Hash Algorithm Classes</span></strong></h3>
<p>Lets have a look at given table for various Hash Algorithm Classes.</p>
<table>
<tbody>
<tr>
<th>Classes</th>
<th>Description</th>
</tr>
<tr>
<th><strong>MD5</strong></th>
<th>
<ol>
<li><span style="font-size:13px;line-height:19px;">Abstract Class extended by a single Concrete class</span></li>
<li><span style="font-size:13px;line-height:19px;">Used to implement Message Digest hashing algorithm Version 5</span></li>
<li><span style="font-size:13px;line-height:19px;">Support 128-bit hash values</span></li>
<li><span style="font-size:13px;line-height:19px;">Widely deployed and commonly used to verify file Integrity</span></li>
<li><span style="font-size:13px;line-height:19px;">Created by Ron Rivest in 1991 to replace MD4</span></li>
</ol>
</th>
</tr>
<tr>
<th><strong>SHA1</strong></th>
<th>
<ol>
<li><strong style="font-size:13px;line-height:19px;">Abstract Class extended by a single Concrete class</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Used to implement SHA1 hashing algorithm</strong></li>
<li><strong style="font-size:13px;line-height:19px;">created to replace MD5 due to flaws</strong></li>
<li><strong style="font-size:13px;line-height:19px;">created in 1995 by NSA as U.S Government Standard</strong></li>
</ol>
</th>
</tr>
<tr>
<th><strong>KeyedHashAlgorithm</strong></th>
<th>
<ol>
<li><strong style="font-size:13px;line-height:19px;">Abstract Class extended by a several Concrete class</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Used to implement the Keyed-Hash Message Authentication Code</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Created by Mihir Bellare, Ran Canetti and Hugo Krawczyk in 1996</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Uses an existing hashing algorithm such as SA1 and then iterates twice to doubly hash the output value.</strong></li>
</ol>
</th>
</tr>
<tr>
<th><strong>RIPEMD160</strong></th>
<th>
<ol>
<li><strong style="font-size:13px;line-height:19px;">Abstract Class extended by a single Concrete class</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Used to implement RACE Integrity Primitive Evaluation Message Digest hashing algorithm</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Created in Europe by Hans Dobbertin, Antoon Bosselaers and Bart Preneel in 1996</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Imroved Version of MD5</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Similar in performance to SHA1</strong></li>
</ol>
</th>
</tr>
<tr>
<th><strong>SHA256</strong></th>
<th>
<ol>
<li><strong style="font-size:13px;line-height:19px;">Abstract Class extended by a single Concrete class</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Used to implement SHA-256 hashing algorithm</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Part of SHA hashing algorithm version 2 variants</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Implement a 256-bit hashing key</strong></li>
</ol>
</th>
</tr>
<tr>
<th><strong>SHA384</strong></th>
<th>
<ol>
<li><strong style="font-size:13px;line-height:19px;">Abstract Class extended by a single Concrete class</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Used to implement the SHA-384 hashing algorithm</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Part if SHA hashing algorithm version 2 variants</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Implements a 384-bit hashing key</strong></li>
</ol>
</th>
</tr>
<tr>
<th><strong>SHA512</strong></th>
<th>
<ol>
<li><strong style="font-size:13px;line-height:19px;">Abstract Class extended by a single Concrete class</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Used to implement the SHA-512 hashing algorithm</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Part of SHA hashing algorithm version 2 variants</strong></li>
<li><strong style="font-size:13px;line-height:19px;">Implements a 512-bit hashing key</strong></li>
</ol>
</th>
</tr>
</tbody>
</table>
<p><strong>Table Content-Reference :</strong> <span style="text-decoration:underline;"><span style="color:#000000;"><em>MS Workshop Advanced Foundation of Microsoft .Net Development.(Book)</em></span></span></p>
<p>As we alrady discus about Hashing by using MD5 class in previous article so here we will go through SHA1 class.</p>
<h3><strong><span style="color:#800000;">Hashing By Using SHA1 Class</span></strong></h3>
<p>just due to the flaws found in the MD5 hashing algorithm, the SHA1 is considered a better hashing algorithm choice, and the successor of MD5, SHA1 proved that it offers better performance and a more secure hashing algorithm than MD5.</p>
<h3><strong><span style="color:#800000;">What is SHA1 Class</span></strong></h3>
<p>The SHA1 is one of the Commonly used hasing algorithm which provide Hashing functionality by using SHA1 classes and extending HashAlgorithm classes. The .Net Framework SHA1 hashing algorihm class uses a 160-bit hash key is extended by the SHA1CryptoServiceProvider(CSP) class and the SHA1Managed class.</p>
<p>Note : The SHA1Managed class is a CLR managed based clas which extends the SHA1 class. it provides the same functionality as the SHACryptoServuceProvider class but being a CLR managed class.<br />
Benefit : It performs better, protects data stored in memory and its nicely cleaned up by the garbage collector.</p>
<p><strong><span style="color:#800000;">Implementation of SHA1 class</span></strong><br />
Here given code show you how to use SHA1CryptoserviceProvider for hashing your data. GetSHA1CSPHash is a function which need values as parameter. In function SHA1provider is an instance of SHA1CryptoserviceProvider Class. after creating instance Convert the input string to a byte array and compute the hash, at last managing Loop through each byte of the hashed data and format each one as a hexadecimal string</p>
<p><em><strong>Note : To access SHA1CryptoserviceProvider class you must need to mention System.Security.Cryptography Namespace.</strong></em></p>
<pre class="brush: csharp; title: ; notranslate">
using System.Security.Cryptography;
</pre>
<p><strong><span style="color:#800000;">Now create a function</span></strong></p>
<pre class="brush: csharp; title: ; notranslate">
public static string GetSHA1CSPHash(string plaintext)
        {
            SHA1CryptoServiceProvider SHA1provider = new SHA1CryptoServiceProvider();
            byte[] hasedvalue = SHA1provider.ComputeHash(Encoding.Default.GetBytes(plaintext));
            StringBuilder str = new StringBuilder();
            for (int counter = 0; counter &lt; hasedvalue.Length; counter++)
            {
                str.Append(hasedvalue[counter].ToString(&quot;X1&quot;));
            }
            return str.ToString();
        }
</pre>
<p>In Same way you can create function for Hashing by SHA1Managed Class. you just need to use SHA1Managed class instead of SHA1CryptoServiceProvider Class. You can create Instance like</p>
<pre class="brush: csharp; title: ; notranslate">
SHA1Managed SHA1provider = new SHA1Managed();
</pre>
<p><strong><span style="color:#800000;">How to use Function</span></strong><br />
you can simply use function by just passing value which need to be hashed as given sample of code.</p>
<p><span style="color:#800000;">What Output comes</span><br />
<a href="http://raviranjankr.files.wordpress.com/2012/10/sha1hash.png"><img class="aligncenter size-full wp-image-854" title="SHA1Hash" alt="" src="http://raviranjankr.files.wordpress.com/2012/10/sha1hash.png?w=640"   /></a></p>
<p><strong><span style="color:#800000;">How to Verify Hash</span></strong></p>
<pre class="brush: csharp; title: ; notranslate">
 static bool VerifySHA1CSPhash(string PlainText, string prevhashedvalue)
        {

            string hashedvalue2 = GetSHA1CSPHash(PlainText);

            // Create a StringComparer an compare the hashes.
            StringComparer strcomparer = StringComparer.OrdinalIgnoreCase;

            if (strcomparer.Compare(hashedvalue2, prevhashedvalue).Equals(0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
</pre>
<p><strong>Note :</strong> In same manner we can create function to verify Hash which using SHA1Managed class. We just need to corresponding function responsible to hashed data.</p>
<p>we can use given codes to learn how to use both function to check hashed function is same or not.</p>
<pre class="brush: csharp; title: ; notranslate">
  static void Main(string[] args)
        {
            Console.WriteLine(&quot;Enter Value for Hashed&quot;);
            string yourvalue = Console.ReadLine();

            string strhashed = GetSHA1CSPHash(yourvalue);
            Console.WriteLine(&quot;Hashed Value : &quot; + strhashed);

            Console.WriteLine(&quot;\nDo you want to verify your hash ? if yes press Y&quot;);
            char ch = Convert.ToChar(Console.ReadLine());
            if (ch == 'Y' || ch == 'y')
            {
                Console.WriteLine(&quot;Enter value again &quot;);
                string yourvalue2 = Console.ReadLine();

                bool res = VerifySHA1CSPhash(yourvalue2, strhashed);
                Console.WriteLine(&quot;--------------------&quot;);
                if (res)
                {

                    Console.WriteLine(&quot;Hash is Matched&quot;);

                }
                else
                {
                    Console.WriteLine(&quot;Hash is not Matching &quot;);
                }
                Console.WriteLine(&quot;--------------------&quot;);
            }
            else { Environment.Exit(1); }

            Console.ReadLine();

        }
</pre>
<p><span style="color:#800000;">What output comes</span><br />
<a href="http://raviranjankr.files.wordpress.com/2012/10/sha1hashver.png"><img class="aligncenter size-full wp-image-855" title="SHA1HashVer" alt="" src="http://raviranjankr.files.wordpress.com/2012/10/sha1hashver.png?w=640"   /></a></p>
<h3><strong><span style="color:#800000;">Download SourceCode</span></strong></h3>
<p>Want to download source code : <a href="http://dl.dropbox.com/u/27553051/SourceCodes/SHA1Hash.zip" target="_blank">Click Me!</a></p>
<h3><strong><span style="color:#800000;">Further Reading</span></strong></h3>
<p><a href="http://en.wikipedia.org/wiki/Hash_function" target="_blank">Hashing Function-[Wikipedia]</a><br />
<a href="http://www.unixwiz.net/techtips/iguide-crypto-hashes.html" target="_blank">An Illustrated Guide to Cryptographic Hashes</a><br />
<a href="http://blogs.msdn.com/b/drnick/archive/2006/05/18/basics-of-encryption-and-hashing.aspx?Redirected=true" target="_blank">Basics of Encryption and Hashing-[MSDN]</a><br />
<a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha1.aspx" target="_blank">SHA1Class-[MSDN]</a></p>
<p><strong><span style="color:#800000;">Coming Next</span></strong></p>
<p>In the next article, I will hopefully come back with more basic details and implementation of HMAC and other Hash Algo classes for beginners. Till then Happy Coding.. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />Filed under: <a href='http://raviranjankr.wordpress.com/category/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/category/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/category/codeproject/'>CodeProject</a>, <a href='http://raviranjankr.wordpress.com/category/cryptography/'>Cryptography</a> Tagged: <a href='http://raviranjankr.wordpress.com/tag/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/tag/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/tag/codeproject/'>CodeProject</a>, <a href='http://raviranjankr.wordpress.com/tag/cryptography/'>Cryptography</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raviranjankr.wordpress.com/847/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raviranjankr.wordpress.com/847/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=847&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raviranjankr.wordpress.com/2012/10/25/cryptography-a-basic-introduction-of-hashing-and-hashalgo-class-for-beginners-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://raviranjankr.files.wordpress.com/2012/10/sha1hashver.png?w=150" />
		<media:content url="http://raviranjankr.files.wordpress.com/2012/10/sha1hashver.png?w=150" medium="image">
			<media:title type="html">SHA1HashVer</media:title>
		</media:content>

		<media:content url="http://0.gravatar.com/avatar/c33505bacfac61a7a64b55a03706ca80?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raviranjankr</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/10/sha1hash.png" medium="image">
			<media:title type="html">SHA1Hash</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/10/sha1hashver.png" medium="image">
			<media:title type="html">SHA1HashVer</media:title>
		</media:content>
	</item>
		<item>
		<title>Cryptography : A Basic Introduction of Hashing and HashAlgo class for beginners &#8211; Part 1</title>
		<link>http://raviranjankr.wordpress.com/2012/10/20/cryptography-a-basic-introduction-of-hashing-and-hashalgo-class-for-beginners-part-1/</link>
		<comments>http://raviranjankr.wordpress.com/2012/10/20/cryptography-a-basic-introduction-of-hashing-and-hashalgo-class-for-beginners-part-1/#comments</comments>
		<pubDate>Sat, 20 Oct 2012 07:01:00 +0000</pubDate>
		<dc:creator>Ravi Ranjan Kumar</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Cryptography]]></category>

		<guid isPermaLink="false">http://raviranjankr.wordpress.com/?p=815</guid>
		<description><![CDATA[Hashing Hash is a kind of process, signature, function which is responsible for translating information into a cryptic value. The concept of hash and encryption is almost same. In practical view Hash is an algorithm that takes an arbitrary block &#8230; <a href="http://raviranjankr.wordpress.com/2012/10/20/cryptography-a-basic-introduction-of-hashing-and-hashalgo-class-for-beginners-part-1/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=815&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h3><strong><span style="color:#800000;">Hashing</span></strong></h3>
<p>Hash is a kind of process, signature, function which is responsible for translating information into a cryptic value. The concept of hash and encryption is almost same. In practical view Hash is an algorithm that takes an arbitrary block of data and returns a fixed-size bit string. Hashing is also known for its unidirectional process because it is not require dehashing or decrypting to get back data. In hashing the data which is needed to be encoded is often called the &#8220;message,&#8221; and the outcome of hash value after processing is sometimes called the message digest or simply digests.<br />
While hashing message, an algorithm is utilize which work is to map input values to a series of known output values. so given then same series of input values, a hash algorithm always produces the same output values. Hashing is an industry supported standard similar to encryption.</p>
<div id="attachment_816" class="wp-caption aligncenter" style="width: 650px"><a href="http://raviranjankr.files.wordpress.com/2012/10/rrk-hashfunction.gif"><img class="size-full wp-image-816" title="RRK-HashFunction" alt="" src="http://raviranjankr.files.wordpress.com/2012/10/rrk-hashfunction.gif?w=640&#038;h=220" height="220" width="640" /></a><p class="wp-caption-text">Animation Pic &#8211; Hash Function</p></div>
<p>While creating and designing hash functions, we generally came across with ways:</p>
<ul>
<li>First one is we have nothing idea and info about the distribution of the incoming keys.<br />
In such situation, hash function proved its importance because its evenly distributes the key range across the hash table.</li>
<li>Second one is we have little bit idea and info about the distribution of the incoming keys.<br />
In such situation, we should use a distribution-dependent hash function that can avoids assigning clusters of related key values to the same hash table slot.</li>
</ul>
<h4><strong><span style="color:#800000;">Practical working of Hashing</span></strong></h4>
<p>As we know Hashing takes any amount of values(Plain Text and Binary) and then creates a constant-length hash representing a checksum for the data so in sense hashing something is a way of turning something (usually a key or a password) into a (usually fixed length string of characters.</p>
<p>Let’s take an example which explains the working process of hashing.<br />
We all daily log on our system either windows/linux/Mac/etc we have to authenticate our self in case if password is set for login. So what we think our password is kept private and secure so no one can trace or stole it. Lots of question in mind like</p>
<p><span style="color:#800000;">How we get authenticated by entering password ?</span><br />
To know the solution of above question we should know the working process of hashing. While creating user account the password which is given by user is not stored by System. Actually a hash value of password is stored. Its all gone through consistent Hashing algorithm to hash plain text to hashed value. So whenever a user enters a password for authentication purpose, this password is not transported or stored. A hash of the password is transported for authentication instead of transporting the password in clear text. System Hashes the password entered by user by using the same hashing algorithm that was used while creating password when user create account.<br />
It is clear System compares the entered hash value while login to the hash value stored while creating account. If the hashes matched the user is authenticated otherwise not authenticated.</p>
<div id="attachment_817" class="wp-caption aligncenter" style="width: 540px"><a href="http://raviranjankr.files.wordpress.com/2012/10/rrk-exmphash.gif"><img class="size-full wp-image-817" title="RRK-exmphash" alt="" src="http://raviranjankr.files.wordpress.com/2012/10/rrk-exmphash.gif?w=640"   /></a><p class="wp-caption-text">Animated Pic &#8211; Flow Chart Example of Hash work</p></div>
<p><span style="color:#800000;">Should we want to hash everything?</span><br />
No doubt, it’s a common assumption to use Hash to secure everything because of its powerful algorithm and way of hashing data’s to fixed constant length.<br />
Why not we can but to make hashing more secure, we should always use salt values. We must also store salt value and keep it confidential and secure. So in such case whenever a hashed value needs to be verified, the input value is combined with the salt value and hashed to generate an output value. So then output hashed value is compared to stored hash value. In sense we can hash a our plain text as long as salt is correctly.<br />
For more info, Must read an awesome article<br />
<a href="http://benlog.com/articles/2008/06/19/dont-hash-secrets/" target="_blank">dont hash code</a></p>
<h3><strong><span style="color:#800000;">Is both hashing and Encryption are Same ?</span></strong></h3>
<p><a href="http://raviranjankr.files.wordpress.com/2012/10/hashencrypdiffqtn.jpg"><img class="aligncenter size-medium wp-image-820" title="HashEncrypDiffQtn" alt="" src="http://raviranjankr.files.wordpress.com/2012/10/hashencrypdiffqtn.jpg?w=300&#038;h=180" height="180" width="300" /></a></p>
<h5><em><span style="color:#000000;">Note &#8211; I used Google to get this cartoon pic.</span></em></h5>
<p>Its usually a common reaction or can be a question for beginner who just put their step in Cryptography. Whatever knowledge of hashing is vital for developers to gain a complete understanding of security and cryptography and being beginner we should have almost basic idea about what hash called and how is different from Encryption.<br />
<em>As we read about Encryption in previous article series-<a href="http://raviranjankr.wordpress.com/2012/07/18/cryptography-symmetric-encryption-by-symmetric-algorithm-classes-part-1/" target="_blank">Cryptography : Symmetric Encryption by Symmetric Algorithm Classes–Part 1</a></em> is that Encryption is a scheme where an intelligible text is made unintelligible using a secure key. The security of the encrypted (Cipher) text resides in the key length and decryption process is a difficult without proper knowledge of the key. In sense while encryption a plain text using a secret algorithm, and sent to a second party who can decrypt the plain text back because they also has access to this secret algorithm.<br />
where as, hashing is refer as one-way functions that compress arbitrary length strings into fixed short strings. Hash Functions can be designed using block ciphers using a secret key as a parameter along with the message that has to be hashed. The important thing and focus point about a hash value is that it is nearly impossible for any one to derive the original input value without knowing the data used to create the hash value. so we can see Hash working is not like encryption because Encrypting is a proper two-way function. It&#8217;s reversible, you can decrypt the mangled string to get original string if you have the key which is not possible in Hash.</p>
<div id="attachment_822" class="wp-caption aligncenter" style="width: 610px"><a href="http://raviranjankr.files.wordpress.com/2012/10/rrk-encryphashdiff.gif"><img class=" wp-image-822 " title="RRK-encryphashdiff" alt="" src="http://raviranjankr.files.wordpress.com/2012/10/rrk-encryphashdiff.gif?w=600&#038;h=400" height="400" width="600" /></a><p class="wp-caption-text">Animated Pic &#8211; How Encryption and Hash are different</p></div>
<p>For more Info, have a read<br />
<a href="http://stackoverflow.com/questions/4948322/fundamental-difference-between-hashing-and-encryption-algorithms" target="_blank">Fundamental difference between Hashing and Encryption algorithms-StackOverFlow QuestionAnswer</a><br />
<a href="http://www.unixwiz.net/techtips/iguide-crypto-hashes.html#digestonly" target="_blank">Hashes are &#8220;digests&#8221;, not &#8220;encryption&#8221;</a></p>
<h3><strong><span style="color:#800000;">Hash Algorithm Classes</span></strong></h3>
<p>The .Net Framework provides some great classes to implement Hashing. The HashAlgorithm class providesbase functionality for all hashing classes in the .Net Framework. The HashAlgorithm class is an abstract(MustInherit) class that is extended by the SHA1, MD, KeyedHashAlgorithm, RIPEMD160, SHA 256,SHA 384and SHA 512 classes where as each of the class is sued to hash data</p>
<p style="text-align:center;"><a href="http://raviranjankr.files.wordpress.com/2012/10/hashalgoclass.jpg"><img class="aligncenter  wp-image-823" title="HashAlgoClass" alt="" src="http://raviranjankr.files.wordpress.com/2012/10/hashalgoclass.jpg?w=550&#038;h=300" height="300" width="550" /></a></p>
<h3><strong><span style="color:#800000;">Hashing by using MD5 class</span></strong></h3>
<p>The MD5 class is an abstract(means must inherit) class which responsible to provides hashing functionality by using MD5 hashing algorithm.</p>
<p><span style="color:#800000;">What is MD5 class</span><br />
The MD5 hashing algorithm is one of the two most commonly used hashing algorithms. There are known flaws in the algorithm and its known as its great features and its popularity.<br />
in .Net Framework, the MD5 hashing algorithm class uses a 128-bit hash key and is extended by the MD5CryptoServiceProvder class which is also known as CSP class.</p>
<p><span style="color:#800000;">What is MD5CryptoServiceProvder(CSP) class</span><br />
The MDCryptoServiceProvieder class extends the MD5 class. The MDCryptoServiceProvieder class that provides cryptographic service by wrapping unmanaged object that are external to the CLR(Common Language Runtime)</p>
<p><span style="color:#800000;">Implementation of MDCryptoServiceProvieder class</span></p>
<p>Here given code show you how to use MD5CryptoServiceProvider for hashing your data. GetMD5Hash is a function which need values as parameter. In function MD5provider is an instance of MD5CryptoServiceProvider. after creating instance Convert the input string to a byte array and compute the hash, at last managing Loop through each byte of the hashed data and format each one as a hexadecimal string</p>
<p><span style="color:#800000;">Note :</span> <em><span style="color:#000000;">To access MD5CryptoServiceProvider class you must need to mention System.Security.Cryptography Namespace</span></em></p>
<pre class="brush: csharp; title: ; notranslate">using System.Security.Cryptography;</pre>
<p>Now create a function</p>
<pre class="brush: csharp; title: ; notranslate">
public static string GetMD5Hash(string plaintext)
        {
            MD5CryptoServiceProvider MD5provider = new MD5CryptoServiceProvider();
            byte[] hasedvalue = MD5provider.ComputeHash(Encoding.Default.GetBytes(plaintext));
            StringBuilder str = new StringBuilder();
            for (int counter = 0; counter &lt; hasedvalue.Length; counter++)
            {
                str.Append(hasedvalue[counter].ToString(&quot;x2&quot;));
            }
            return str.ToString();
        }
</pre>
<p><span style="text-decoration:underline;"><span style="color:#800000;text-decoration:underline;">How to use Function</span></span><br />
you can simply use function by just passing value which need to be hashed as given sample of code.</p>
<pre class="brush: csharp; title: ; notranslate">
 static void Main(string[] args)
        {
 Console.WriteLine(&quot;Enter Value for Hashed&quot;);
            string yourvalue = Console.ReadLine();

            string strhashed = GetMD5Hash(yourvalue);
            Console.WriteLine(&quot;Hashed Value : &quot; + strhashed);
       }
</pre>
<p><span style="text-decoration:underline;"><span style="color:#800000;text-decoration:underline;">What Output comes</span></span><br />
<a href="http://raviranjankr.files.wordpress.com/2012/10/hashmd5.png"><img class="aligncenter size-full wp-image-830" title="HashMD5" alt="" src="http://raviranjankr.files.wordpress.com/2012/10/hashmd5.png?w=640"   /></a></p>
<p><span style="text-decoration:underline;"><span style="color:#800000;text-decoration:underline;">How to Verify Hash</span></span></p>
<p>To Verify a hash against a string we can use StringComparer class which responsible to represents a string comparison operation that uses specific case and culture-based or ordinal comparison rules.</p>
<pre class="brush: csharp; title: ; notranslate">
static bool VerifyMD5hash(string PlainText, string prevhashedvalue)
        {

            string hashedvalue2 = GetMD5Hash(PlainText);

            // Create a StringComparer an compare the hashes.
            StringComparer strcomparer = StringComparer.OrdinalIgnoreCase;

            if (strcomparer.Compare(hashedvalue2, prevhashedvalue).Equals(0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
</pre>
<p>we can use given codes to learn how to use both function to check hashed function is same or not.</p>
<pre class="brush: csharp; title: ; notranslate">
static void Main(string[] args)
        {
            Console.WriteLine(&quot;Enter Value for Hashed&quot;);
            string yourvalue = Console.ReadLine();

            string strhashed = GetMD5Hash(yourvalue);
            Console.WriteLine(&quot;Hashed Value : &quot; + strhashed);

            Console.WriteLine(&quot;\nDo you want to verify your hash ? if yes press Y&quot;);
            char ch = Convert.ToChar(Console.ReadLine());
            if (ch == 'Y' || ch == 'y')
            {
                Console.WriteLine(&quot;Enter value again &quot;);
                string yourvalue2 = Console.ReadLine();

                bool res = VerifyMD5hash(yourvalue2, strhashed);
                Console.WriteLine(&quot;--------------------&quot;);
                if (res)
                {

                    Console.WriteLine(&quot;Hash is Matched&quot;);

                }
                else
                {
                    Console.WriteLine(&quot;Hash is not Matching &quot;);
                }
                Console.WriteLine(&quot;--------------------&quot;);
            }
            else { Environment.Exit(1); }
         }
</pre>
<p><span style="text-decoration:underline;"><span style="color:#800000;text-decoration:underline;">What output comes</span></span> <a href="http://raviranjankr.files.wordpress.com/2012/10/hashmd52.png"><img class="aligncenter size-full wp-image-831" title="HashMD52" alt="" src="http://raviranjankr.files.wordpress.com/2012/10/hashmd52.png?w=640"   /></a><a href="http://raviranjankr.files.wordpress.com/2012/10/hashmd53.png"><img class="aligncenter size-full wp-image-832" title="HashMD53" alt="" src="http://raviranjankr.files.wordpress.com/2012/10/hashmd53.png?w=640"   /></a></p>
<h3><span style="color:#800000;">Download SourceCode</span></h3>
<p>Want to download source code : <a href="http://dl.dropbox.com/u/27553051/SourceCodes/MD5Hash.zip" target="_blank">Click Me!</a></p>
<h3><strong><span style="color:#800000;">Further Reading</span></strong></h3>
<p><a href="http://en.wikipedia.org/wiki/Hash_function" target="_blank">Hashing Function-[Wikipedia]</a><br />
<a href="http://www.unixwiz.net/techtips/iguide-crypto-hashes.html" target="_blank">An Illustrated Guide to Cryptographic Hashes</a><br />
<a href="http://blogs.msdn.com/b/drnick/archive/2006/05/18/basics-of-encryption-and-hashing.aspx?Redirected=true" target="_blank">Basics of Encryption and Hashing-[MSDN] </a><br />
<a href="http://benlog.com/articles/2008/06/19/dont-hash-secrets/" target="_blank">Don&#8217;t Hash Secret</a></p>
<h3><strong><span style="color:#800000;">Coming Next</span></strong></h3>
<p>In the next article, I will hopefully come back with more basic details and implementation of SHA1 and other Hash Algo classes for beginners. Till then Happy Coding&#8230; <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />Filed under: <a href='http://raviranjankr.wordpress.com/category/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/category/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/category/codeproject/'>CodeProject</a>, <a href='http://raviranjankr.wordpress.com/category/cryptography/'>Cryptography</a> Tagged: <a href='http://raviranjankr.wordpress.com/tag/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/tag/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/tag/codeproject/'>CodeProject</a>, <a href='http://raviranjankr.wordpress.com/tag/cryptography/'>Cryptography</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raviranjankr.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raviranjankr.wordpress.com/815/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=815&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raviranjankr.wordpress.com/2012/10/20/cryptography-a-basic-introduction-of-hashing-and-hashalgo-class-for-beginners-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://raviranjankr.files.wordpress.com/2012/10/rrk-encryphashdiff.gif?w=150" />
		<media:content url="http://raviranjankr.files.wordpress.com/2012/10/rrk-encryphashdiff.gif?w=150" medium="image">
			<media:title type="html">RRK-encryphashdiff</media:title>
		</media:content>

		<media:content url="http://0.gravatar.com/avatar/c33505bacfac61a7a64b55a03706ca80?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raviranjankr</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/10/rrk-hashfunction.gif" medium="image">
			<media:title type="html">RRK-HashFunction</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/10/rrk-exmphash.gif" medium="image">
			<media:title type="html">RRK-exmphash</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/10/hashencrypdiffqtn.jpg?w=300" medium="image">
			<media:title type="html">HashEncrypDiffQtn</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/10/rrk-encryphashdiff.gif?w=300" medium="image">
			<media:title type="html">RRK-encryphashdiff</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/10/hashalgoclass.jpg?w=300" medium="image">
			<media:title type="html">HashAlgoClass</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/10/hashmd5.png" medium="image">
			<media:title type="html">HashMD5</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/10/hashmd52.png" medium="image">
			<media:title type="html">HashMD52</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/10/hashmd53.png" medium="image">
			<media:title type="html">HashMD53</media:title>
		</media:content>
	</item>
		<item>
		<title>A Basic Introduction of System Types for Beginners &#8211; Part 1</title>
		<link>http://raviranjankr.wordpress.com/2012/10/12/a-basic-introduction-of-system-types-for-beginners-part-1/</link>
		<comments>http://raviranjankr.wordpress.com/2012/10/12/a-basic-introduction-of-system-types-for-beginners-part-1/#comments</comments>
		<pubDate>Fri, 12 Oct 2012 15:19:02 +0000</pubDate>
		<dc:creator>Ravi Ranjan Kumar</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>

		<guid isPermaLink="false">http://raviranjankr.wordpress.com/?p=794</guid>
		<description><![CDATA[Introduction System types plays an important role in building .NET framework application either Application is Windows or Web or Integer data returned by a web services method. All are using .Net framework system type. It’s impossible to create any .Net &#8230; <a href="http://raviranjankr.wordpress.com/2012/10/12/a-basic-introduction-of-system-types-for-beginners-part-1/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=794&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h2><span style="color:#800000;">Introduction</span></h2>
<p>System types plays an important role in building .NET framework application either Application is Windows or Web or Integer data returned by a web services method. All are using .Net framework system type. It’s impossible to create any .Net Framework application without using System types. So we can understand its necessity in .Net Framework.<br />
In addition to System types provided by .Net Framework, we can also create custom (user defined) system types like classes and structures and Interfaces to mention or defined functionality.</p>
<p>In .Net Framework, all functionality are encapsulated into objects, where objects are instances of system type classes. However .Net framework composes itself many predefined system types which is known as base system types. These base types enable you to create application easily and rapidly because of its encapsulating nature of common tasks. Base System types represent a set of predefined data types. Based on the system types in a development environment, you can classify an environment as loosely typed or strongly typed. A loosely typed development environment provides you the flexibility to store different types of data inside a variable. A strongly typed environment ensures that all the rules applicable to the given system type are adhered to.</p>
<h3><strong><span style="color:#800000;">System Types:</span></strong></h3>
<p>From <a href="http://en.wikibooks.org/wiki/.NET_Development_Foundation/System_Types_Topics#System_types" target="_blank">wikibooks</a> &#8211; Types are a way to classify the concepts or objects of a language. The way this classification is organized is called a &#8216;type system&#8217;. The types themselves can also be categorized in different ways by the type system. The first way to categorize types in .NET is to make a difference between types that are part of the framework class libraries (System types) and types that will be constructed by the developer (custom types).<br />
There are many ways to categorize types in .NET Framework. One of them is by the way the objects created based on those types are mapped to the computer memory. This will give us Value types and Reference types.</p>
<p>We all know software development environment are classified on the basis of how the compilers manage data types. The two types of well-known compiler are</p>
<ul>
<li>strongly typed environment and</li>
<li>loosely typed environment.</li>
</ul>
<p>Lets take a look of basic info for Strongly and loosely typed environment.</p>
<h3><span style="color:#800000;">What is Loosely typed environment –</span></h3>
<ul>
<li>Cannot enforce type safety.</li>
<li>Cannot guarantee that there is one and only one particular type of data in a location memory or a variable in given time.</li>
<li>Don’t use memory very efficiently because a variable accommodates any possible types of value.</li>
</ul>
<h3><span style="color:#800000;">What is strongly typed environment -</span></h3>
<ul>
<li>Can ensure data type safety.</li>
<li>Can give guarantee that there is one and only one particular type of data in a location or a variable in given time.</li>
<li>Use memory very efficiently.</li>
</ul>
<h3><strong><span style="color:#800000;">What is Common Type System</span></strong></h3>
<p>The .Net Framework provides multiple language support using the features known as Common Type System that is built into the <strong>CLR(Common Language RunTime)</strong> The CTS support a variety and operation found in most programming language and therefore calling one language from another does not require type conversions.Although C# is specially designed for the .Net platform, we can built .Net programs in a number of other languages including C++ and Visual Basic.</p>
<ul>In short, .Net Framework provide common set of data types called Common Type System.</ul>
<p>Programing language that support CTS can easily communicate with each other.<br />
All System types in CTS derive from the same base type, which is <strong>System.Object</strong>. Both categories of System in the .Net Framework, value types and reference type display common intrinsic behavior because they derive from <strong>System.Object</strong>.<br />
For more reference, have a read<br />
<a href="http://en.wikipedia.org/wiki/Common_Type_System">Common Type System-[Wikipedia]</a><br />
<a href="http://msdn.microsoft.com/en-us/library/zcx1eb1e%28v=vs.100%29.aspx">Common Type System-[MSDN]<br />
</a></p>
<h3><span style="color:#800000;">Working with Special System Types</span></h3>
<p>As we discussed and learned about base system types which enable us to develop robust .Net Framework application easily and rapidly. The .Net Framework encapsulated some special types that take .Net Framework application further with better type safety, fewer bugs and better customizability. These special system types are generics, Nullable types, exception classes and attributes.<br />
Generic is fairly and highly useful concept of .Net Framework. In earlier version of .Net framework, objects are added to collection after implicit casting to the <strong>System.Object</strong> type. This resulting for compromising type safety. In the .Net Framework you can see the predefined collections and Interfaces that generic provide to create collections objects or define custom types that are type-safe. Whereas Nullable which is also a special System Type used to declare value types that can store null values. Now we cannot forget about other system types which exception classes and attributed. We can use exception classes to handle error conditions during run time whereas we can use attributed to convey information to the rim time about the behavior or programmatic elements, such as classes, enumerators and assemblies.</p>
<h3><strong><span style="color:#800000;">Generics :</span></strong></h3>
<p><span style="color:#800000;">What are Generics :-<br />
</span></p>
<ul>
<li>Generics are the most powerful and prominent feature of C#</li>
<li>Generics allow you to define type-safe data structures, without committing to actual data types.</li>
<li>The System.Collections.Generics namespace contains the generics collections.</li>
<li>The most common use of generics is to create collection classes.</li>
<li>Generic classes may be constrained to enable access to methods on particular data types.</li>
<li>Generics can act like any data type means there is no need to write any internal code</li>
<li>Generics can be refer to a method ,class, structure or an interface in a type safe manner</li>
<li>We can create your own generic interfaces, classes, methods, events and delegates.</li>
</ul>
<p><span style="color:#800000;">Implementation of generics :-<br />
</span></p>
<p>The code examples defines a generic class called CommmonData, which is used to create two object. one to store string value and other to store int value. The CommonData class ensures type safety by accepting the required type in its constructor. This implies that value specified for an object of the CommonData class is of same type as that specified while creating the corresponding object.</p>
<pre class="brush: csharp; title: ; notranslate">
 class Program
    {
        static void Main(string[] args)
        {
            // Declare a list of type string
            CommonData name = new CommonData();
            name.Value = &quot;Cricket Team&quot;;

            // Declare a list of type int
            CommonData member = new CommonData();
            member.Value = 15;
            Console.WriteLine(&quot;Team Name :&quot; + name.Value);
            Console.WriteLine(&quot;Number of Member : &quot;+ member.Value);
            Console.ReadLine();

        }
        // Declare the generic class
        public class CommonData
        {
            private T data;
            public T Value
            {
                get { return this.data; }
                set { this.data = value; }
            }
        }
    }
</pre>
<p><span style="color:#800000;">What are Advantage of generics :-<br />
</span></p>
<p><strong>Reusability</strong> : A single generic type definition can be used for multiple scenarios in the same code, without any alterations.<br />
<strong>Type Safety :</strong> Generic data type provide better type safety, especially in situations where collections are used.<br />
<strong>Performance :</strong> Generic types perform better than normal system types because they reduce the need boxing, unboxing and type casting the variable or objects.</p>
<p>for more information, have a read<br />
<a href="http://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx">An Introduction to Generics </a><br />
<a href="http://msdn.microsoft.com/en-us/library/512aeb7t%28v=vs.80%29.aspx">Generics-[MSDN] </a></p>
<h3><strong><span style="color:#800000;">Coming Next </span></strong></h3>
<p>In the next article, I will come back with more detail and implementation of Generics, basic introduction of Value Type and Reference Type, how null values are assigned to value types and how exception classes are used for handling exceptions.</p>
<br />Filed under: <a href='http://raviranjankr.wordpress.com/category/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/category/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/category/codeproject/'>CodeProject</a> Tagged: <a href='http://raviranjankr.wordpress.com/tag/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/tag/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/tag/codeproject/'>CodeProject</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raviranjankr.wordpress.com/794/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raviranjankr.wordpress.com/794/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=794&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raviranjankr.wordpress.com/2012/10/12/a-basic-introduction-of-system-types-for-beginners-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c33505bacfac61a7a64b55a03706ca80?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raviranjankr</media:title>
		</media:content>
	</item>
		<item>
		<title>Cryptography : Asymmetric Encryption by using Asymmetric Algorithm Classes.</title>
		<link>http://raviranjankr.wordpress.com/2012/08/27/cryptography-asymmetric-encryption-by-using-asymmetric-algorithm-classes/</link>
		<comments>http://raviranjankr.wordpress.com/2012/08/27/cryptography-asymmetric-encryption-by-using-asymmetric-algorithm-classes/#comments</comments>
		<pubDate>Mon, 27 Aug 2012 16:54:17 +0000</pubDate>
		<dc:creator>Ravi Ranjan Kumar</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Cryptography]]></category>

		<guid isPermaLink="false">http://raviranjankr.wordpress.com/?p=759</guid>
		<description><![CDATA[In previous blog – Symmetric Encryption by Symmetric Algorithm Classes–Part 1 and Part 2 we have learned about basic introduction of Cryptography based on Symmetric Encryption. so, now In addition to previous blog here we will learn about basics of &#8230; <a href="http://raviranjankr.wordpress.com/2012/08/27/cryptography-asymmetric-encryption-by-using-asymmetric-algorithm-classes/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=759&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<address>In previous blog – <a href="http://raviranjankr.wordpress.com/category/cryptography/" target="_blank">Symmetric Encryption by Symmetric Algorithm Classes–Part 1 and Part 2</a> we have learned about basic introduction of Cryptography based on Symmetric Encryption. so, now In addition to previous blog here we will learn about basics of Asymmetric Encryption.</address>
<h2><span style="color:#800000;">Asymmetric Encryption :</span></h2>
<p>Asymmetric encryption is also referred to as <span style="color:#800000;">public key</span> encryption because it uses public key as well as private key. means Its having a secret key that must be kept from unauthorized or anonymous users and a public key that can make public to any one. <strong>Hence we can say that Asymmetric encryption is designed so that the private key remains shield and secret, whereas the publick key is widely distributed.</strong> The private key is used to lock information, whereas the public key is used to unlock it.<br />
The main benefit of opting asymmetric encryption is that you can share encrypted data without having access to the private key. In standard mode asymmetric encryption is used more commonly than symmetric encryption, and it is proved that its a standard used to help secure communication over the Internet.<br />
Have a look at animated pic to view its working</p>
<div id="attachment_760" class="wp-caption aligncenter" style="width: 618px"><a href="http://raviranjankr.files.wordpress.com/2012/08/asymmetric-encryption.gif"><img class="size-full wp-image-760" title="Asymmetric Encryption" src="http://raviranjankr.files.wordpress.com/2012/08/asymmetric-encryption.gif?w=640" alt=""   /></a><p class="wp-caption-text">Animated Pic:- Asymmetric Encryption Decryption Concept</p></div>
<p>we came to know in earlier blog about symmetric encryption, the same key is used for both encryption and decryption, however this approach is simpler but less secure since the key must be communicated to and known at both sender and receiver locations.<br />
<span style="text-decoration:underline;"><strong><span style="color:#800000;text-decoration:underline;">Example For Better Understand :</span></strong></span><br />
Lets assume about conversion of Plain text between A and B. If <span style="color:#800000;">&#8216;A&#8217;</span> send message to <span style="color:#800000;">&#8216;B&#8217;</span>, <span style="color:#800000;">&#8216;A&#8217;</span> can find out public key (but not private key of <span style="color:#800000;">&#8216;B&#8217;</span>) of <span style="color:#800000;">&#8216;B&#8217;</span> from a central administrator and encrypt a message to <span style="color:#800000;">&#8216;B&#8217;</span> using <span style="color:#800000;"><strong>&#8216;B&#8217;</strong></span> public key. When <span style="color:#800000;">&#8216;B&#8217;</span> receive it, <span style="color:#800000;">&#8216;B&#8217; <span style="color:#000000;">can</span></span> decrypt it with <span style="color:#800000;">&#8216;B&#8217;</span> private key. In addition to encrypting messages (which can ensures privacy), <span style="color:#800000;">&#8216;B&#8217;</span> can authenticate itself to <span style="color:#800000;">&#8216;A&#8217;</span> (so <span style="color:#800000;">&#8216;A&#8217;</span> know that it is really <span style="color:#800000;">&#8216;B&#8217;</span> who is the sender of message to <span style="color:#800000;">&#8216;A&#8217;</span>) by using <span style="color:#800000;">&#8216;B&#8217;</span> private key to encrypt a digital certificate. When <span style="color:#800000;">&#8216;A&#8217;</span> receive it, <span style="color:#800000;">&#8216;A&#8217;</span> can use <span style="color:#800000;">&#8216;B&#8217;</span> public key to decrypt it.</p>
<h2><span style="color:#800000;">Asymmetric Algorithm Classes</span></h2>
<pre class="brush: csharp; title: ; notranslate">
System.Security.Cryptography
</pre>
<p>namespace provides encryption classes that provide most popular Asymmetric algorithms like</p>
<ul>
<li>RSA and RSACryptoServiceProvider</li>
<li>DSA and DSACryptoServiceProvider</li>
</ul>
<h2><span style="color:#800000;">RSACryptoServiceProvider Class</span></h2>
<p>RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman, who first publicly described it in 1977-<strong>[ <a href="http://en.wikipedia.org/wiki/RSA_(algorithm)" target="_blank">From Wikipedia</a> ]</strong></p>
<p>The RSA class is an abstract class that extends the Asymmetric Algorithm class and provides support for the RSA algorithm. The .NET Framework RSA algorithm support an encryption key size ranging from 384 bits to 16,384 bits in increments of 8 bits by using the Microsoft Enhanced Cryptographic Provider and an encryption key size ranging from 384 bits to 512 bits in increments of 8 bits by using the Microsoft Base Crystallographic Provider.</p>
<p>The RSACryptoServiceProvider class exnteds the RSA class and is the concrete RSA algorithm class.</p>
<p><strong><span style="text-decoration:underline;color:#800000;">Implementation of RSACryptoServiceProvider Class</span></strong></p>
<p>To perform Encryption and Decryption. you must add</p>
<pre class="brush: csharp; title: ; notranslate">
using System.Security.Cryptography; // Namespace
</pre>
<p>Now take a look at encryption function</p>
<pre class="brush: csharp; title: ; notranslate">
 static public byte[] RSAEncrypt(byte[] byteEncrypt, RSAParameters RSAInfo, bool isOAEP)
        {
            try
            {
                byte[] encryptedData;
                //Create a new instance of RSACryptoServiceProvider.
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {

                    //Import the RSA Key information. This only needs
                    //toinclude the public key information.
                    RSA.ImportParameters(RSAInfo);

                    //Encrypt the passed byte array and specify OAEP padding.
                      encryptedData = RSA.Encrypt(byteEncrypt, isOAEP);
                }
                return encryptedData;
            }
            //Catch and display a CryptographicException
            //to the console.
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);

                return null;
            }

        }
</pre>
<p>In above code <span style="color:#800000;">Encrypt</span> Function is used to encrypt plain text to cipher text. Encrypt Function Need two parameter first one is byte array of plain text and second one specify <span style="color:#800000;">OAEP padding(True or False)</span></p>
<p>Now in same way we need to create function for Decrypt the PlainText(Encrypted Text)<br />
have a look at given function which is responsible to decrypt encrypted text.</p>
<pre class="brush: csharp; title: ; notranslate">
  static public byte[] RSADecrypt(byte[] byteDecrypt, RSAParameters RSAInfo, bool isOAEP)
        {
            try
            {
                byte[] decryptedData;
                //Create a new instance of RSACryptoServiceProvider.
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {
                    //Import the RSA Key information. This needs
                    //to include the private key information.
                    RSA.ImportParameters(RSAInfo);

                    //Decrypt the passed byte array and specify OAEP padding.
                      decryptedData = RSA.Decrypt(byteDecrypt, isOAEP);
                }
                return decryptedData;
            }
            //Catch and display a CryptographicException
            //to the console.
            catch (CryptographicException e)
            {
                Console.WriteLine(e.ToString());

                return null;
            }
</pre>
<p>we can see in above code <span style="color:#800000;">Decrypt</span> Function is used in same manner to decrypt cipher text to plain text. <span style="color:#800000;">Decrypt</span> Function Need two parameter first one is byte array of encrypted text and second one specify <span style="color:#800000;">OAEP padding(True or False)</span></p>
<p><strong>Note :-</strong> <a href="http://en.wikipedia.org/wiki/Optimal_asymmetric_encryption_padding" target="_blank">OAEP padding</a> is only available on Microsoft Windows XP or later.</p>
<p>Now we have created function, so we can use both function to appropriate manner to accomplishment of Encryption Decryption task.</p>
<p><strong>Note : </strong>we need to access RSACryptoServiceProvider class here.</p>
<pre class="brush: csharp; title: ; notranslate">
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
</pre>
<p><span style="text-decoration:underline;"><strong><span style="color:#800000;text-decoration:underline;">How to use Encrypt and Decryption Function</span></strong></span></p>
<p><strong>Note : below code is tested in Windows Application. You can Download Source Code for better understand.</strong></p>
<pre class="brush: csharp; title: ; notranslate">
UnicodeEncoding ByteConverter = new UnicodeEncoding();
        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
        byte[] plaintext;
        byte[] encryptedtext;
</pre>
<p><span style="text-decoration:underline;"><strong><span style="color:#800000;text-decoration:underline;">For Encrypt Text</span></strong></span></p>
<pre class="brush: csharp; title: ; notranslate">
 plaintext = ByteConverter.GetBytes(txtplain.Text);
            encryptedtext = RSAEncrypt(plaintext, RSA.ExportParameters(false), false);
            txtencrypt.Text = ByteConverter.GetString(encryptedtext);
</pre>
<p><span style="text-decoration:underline;"><strong><span style="color:#800000;text-decoration:underline;">For Decrypt Text or Back to Plain Text</span></strong></span></p>
<pre class="brush: csharp; title: ; notranslate">
//While Decryption set True,  the private key information (using RSACryptoServiceProvider.ExportParameters(true),
byte[] decryptedtex = RSADecrypt(encryptedtext, RSA.ExportParameters(true), false);
 txtdecrypt.Text = ByteConverter.GetString(decryptedtex);

</pre>
<p><strong><span style="text-decoration:underline;"><span style="color:#800000;text-decoration:underline;">What we&#8217;ve seen</span></span></strong></p>
<ul>
<li>Create Function of Encryption and Decryption</li>
<li>Create Encoder</li>
<li>Create RSACryptoServiceProvider Instance.</li>
<li>Create byte array to illustrate the encrypted and decrypted data.</li>
<li>encrypted text and Display Cipher text</li>
<li>Decrypt cipher text and Display back to plain text.</li>
</ul>
<p><strong>Output by using RSAEncryption Program.</strong></p>
<div id="attachment_777" class="wp-caption aligncenter" style="width: 650px"><a href="http://raviranjankr.files.wordpress.com/2012/08/rsaencryption1.gif"><img class="size-full wp-image-777" title="RSAEncryption" src="http://raviranjankr.files.wordpress.com/2012/08/rsaencryption1.gif?w=640&#038;h=200" alt="" width="640" height="200" /></a><p class="wp-caption-text">Animated Pic : Screen Shot of RSA Encryption Program.</p></div>
<h2><span style="color:#800000;">Download Source Code :</span></h2>
<p>Want to Download Source Code : <a href="http://dl.dropbox.com/u/27553051/SourceCodes/RSAEncryption.zip" target="_blank">Click Me!</a></p>
<h2><span style="color:#800000;">Further Reading</span></h2>
<p><a href="http://en.wikipedia.org/wiki/RSA_(algorithm)" target="_blank">RSA Algorithm-[Wikipedia]</a><br />
<a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx" target="_blank">RSACryptoServiceProvider Class-[MSDN]</a><br />
<a href="http://msdn.microsoft.com/en-us/library/ff650720.aspx" target="_blank">Data Confidentiality-[MSDN]</a></p>
<p><span style="text-decoration:underline;"><strong><span style="color:#800000;text-decoration:underline;">Coming Next</span></strong></span><br />
Please Stay in touch for extend part of this article. Topic will move around &#8220;Introduction and Implementation of DSACryptoServiceProvider for Beginners&#8221;.</p>
<br />Filed under: <a href='http://raviranjankr.wordpress.com/category/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/category/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/category/codeproject/'>CodeProject</a>, <a href='http://raviranjankr.wordpress.com/category/cryptography/'>Cryptography</a> Tagged: <a href='http://raviranjankr.wordpress.com/tag/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/tag/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/tag/codeproject/'>CodeProject</a>, <a href='http://raviranjankr.wordpress.com/tag/cryptography/'>Cryptography</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raviranjankr.wordpress.com/759/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raviranjankr.wordpress.com/759/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=759&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raviranjankr.wordpress.com/2012/08/27/cryptography-asymmetric-encryption-by-using-asymmetric-algorithm-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://raviranjankr.files.wordpress.com/2012/08/asymmetric-encryption.gif?w=150" />
		<media:content url="http://raviranjankr.files.wordpress.com/2012/08/asymmetric-encryption.gif?w=150" medium="image">
			<media:title type="html">Asymmetric Encryption</media:title>
		</media:content>

		<media:content url="http://0.gravatar.com/avatar/c33505bacfac61a7a64b55a03706ca80?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raviranjankr</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/08/asymmetric-encryption.gif" medium="image">
			<media:title type="html">Asymmetric Encryption</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/08/rsaencryption1.gif" medium="image">
			<media:title type="html">RSAEncryption</media:title>
		</media:content>
	</item>
		<item>
		<title>Cryptography : Symmetric Encryption by Symmetric Algorithm Classes – Part 2</title>
		<link>http://raviranjankr.wordpress.com/2012/08/15/cryptography-symmetric-encryption-by-symmetric-algorithm-classes-part-2/</link>
		<comments>http://raviranjankr.wordpress.com/2012/08/15/cryptography-symmetric-encryption-by-symmetric-algorithm-classes-part-2/#comments</comments>
		<pubDate>Wed, 15 Aug 2012 16:02:46 +0000</pubDate>
		<dc:creator>Ravi Ranjan Kumar</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Cryptography]]></category>

		<guid isPermaLink="false">http://raviranjankr.wordpress.com/?p=719</guid>
		<description><![CDATA[In previous blog &#8211; [ Cryptography : Symmetric Encryption by Symmetric Algorithm Classes – Part 1 ]  we have learned about basic introduction of Cryptography , Symmetric Encryption , Symmetric Encryption Algorithm classes and codes for implementing DES class. so, &#8230; <a href="http://raviranjankr.wordpress.com/2012/08/15/cryptography-symmetric-encryption-by-symmetric-algorithm-classes-part-2/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=719&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In previous blog &#8211; [ <a href="http://raviranjankr.wordpress.com/2012/07/18/cryptography-symmetric-encryption-by-symmetric-algorithm-classes-part-1/" target="_blank">Cryptography : Symmetric Encryption by Symmetric Algorithm Classes – Part 1</a> ]  we have learned about basic introduction of Cryptography , Symmetric Encryption , Symmetric Encryption Algorithm classes and codes for implementing DES class.<br />
so, now In addition to previous blog here we will learn about TripleDES class, RC2 class and Rijndael class.</p>
<h2><span style="color:#800000;">TripleDES class</span></h2>
<p>The TripleDES class is an abstract class that extends the SymmetricAlgorithm class and provides support for the Triple Data Encryption Standard(Triple DES or TDES or TDEA) algorithm. TripleDES is a method kind of using DES(Data Encryption Standard) to provide additional security. TripleDES can be done by using two or with three keys.<br />
TripleDES as its name implies increase encryption strength by applying the DES encryption algorithm to data three times before rendering to a result. Since the algorithm performs an encrypt-decrypt-encrypt sequence, this is sometimes called the EDE(Encryption Decryption Encryption) mode. TripleDES algorithm supports an encryption key up to 168 bits (3*156) in size to 192 bits (Triple DES utilizes three 64-bit keys, 64*3 bits) in increments of 64 bits. Triple DES gives a relatively simple and awesome method of increasing the key size of DES(Data Encryption Standard) to protect against attacks.</p>
<p><a href="http://raviranjankr.files.wordpress.com/2012/08/tripledesanimpic.gif"><img class="aligncenter size-full wp-image-723" title="TripleDESAnimPic" src="http://raviranjankr.files.wordpress.com/2012/08/tripledesanimpic.gif?w=640&#038;h=480" alt="" width="640" height="480" /></a></p>
<p>The following code sample guide you how to use and implement the <a href="http://msdn.microsoft.com/en-us/library/58e3zzbz" target="_blank">TripleDESCryptoServiceProvider</a> class.</p>
<p>Note SourceCode was almost same as we have read in previous article. Just Implantation are different so keep focus on Implementation code.</p>
<p>To perform Encryption and Decryption. you must add</p>
<pre class="brush: csharp; title: ; notranslate">
using System.Security.Cryptography; // Namespace
</pre>
<p>Now take a look at encryption function</p>
<pre class="brush: csharp; title: ; notranslate">
public static byte[] Encrypt(string strText, SymmetricAlgorithm key)
{
// Create a memory stream.
MemoryStream ms = new MemoryStream();
// Create a CryptoStream using the memory stream and the
// CSP(cryptoserviceprovider) DES key.
CryptoStream crypstream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);
// Create a StreamWriter to write a string to the stream.
StreamWriter sw = new StreamWriter(crypstream);
// Write the strText to the stream.
sw.WriteLine(strText);
// Close the StreamWriter and CryptoStream.
sw.Close();
crypstream.Close();
// Get an array of bytes that represents the memory stream.
byte[] buffer = ms.ToArray();
// Close the memory stream.
ms.Close();

// Return the encrypted byte array.
return buffer;
}
</pre>
<p>In above code, CreateEncryptor() function is used to encryptor object with the current Key property and initialization vector.</p>
<p>Now in same way we need to create function for Decrypt the PlainText(Encrypted Text)<br />
have a look at given function which is responsible to decrypt encrypted text.</p>
<pre class="brush: csharp; title: ; notranslate">

public static string Decrypt(byte[] encryptText, SymmetricAlgorithm key)

        {
            // Create a memory stream to the passed buffer.
            MemoryStream ms = new MemoryStream(encryptText);
 // Create a CryptoStream using  memory stream and CSP DES key.
            CryptoStream crypstream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);
         // Create a StreamReader for reading the stream.
            StreamReader sr = new StreamReader(crypstream);
      // Read the stream as a string.
          string val = sr.ReadLine();
           // Close the streams.
            sr.Close();
            crypstream.Close();
            ms.Close();
          return val;
        }
</pre>
<p>In above code, decryption is handled in same way by using CreateDecryptor() function instead of CreateEncryptor()</p>
<p>Now we have created function, so we can use both function to appropriate manner to accomplishment of Encryption Decryption task.</p>
<p><strong>Note : we need to access TripleDESCryptoServiceProvider class here.</strong></p>
<pre class="brush: csharp; title: ; notranslate">TripleDESCryptoServiceProvider key = new TripleDESCryptoServiceProvider(); // Key is the object</pre>
<p><strong><span style="text-decoration:underline;color:#800000;">How to use Encrypt Function</span></strong></p>
<pre class="brush: csharp; title: ; notranslate">
//Creating object of TripleDES class.
TripleDESCryptoServiceProvider key = new TripleDESCryptoServiceProvider();
// Encrypt a string to a byte array.
 byte[] buffer = Encrypt(&quot;ABC&quot;, key); // ABC is an example you can put anytext as your need
</pre>
<p><strong><span style="text-decoration:underline;"><span style="color:#800000;text-decoration:underline;">How to use Decrypt Function</span></span></strong></p>
<pre class="brush: csharp; title: ; notranslate">
//Creating object of TripleDES class.
TripleDESCryptoServiceProvider key = new TripleDESCryptoServiceProvider();
// You can make global variable for holding encrypted byte array value.
byte[] buffer = Encrypt(&quot;ABC&quot;, key);
string strText = Decrypt(buffer, key); // strText will hold the original text back.
</pre>
<h2><span style="color:#800000;">RC2 class</span></h2>
<p>The RC2 classis an abstract class that extends the Symmetric Algorithm class and provides support for the RC2 algorithm.<br />
<a href="http://en.wikipedia.org/wiki/Ron_Rivest" target="_blank">Ron Rivest</a> He is the one who discovered RC2, RC4, RC5, and RC6. Hence RC stands for Ron&#8217;s Codes. however <a href="http://en.wikipedia.org/wiki/Lotus_Software" target="_blank">Lotus Software Company</a> is the backend in creating the whole script of RC2 algorithm.</p>
<h5>Reference Link :- <a href="http://en.wikipedia.org/wiki/RC2" target="_blank"><br />
http://en.wikipedia.org/wiki/RC2<br />
</a></h5>
<p>The RC2CryptoServiceProvider object is a block cipher that encrypts and decrypts data in blocks of 8 bytes. This class pads the final block of data if it is less than 8 bytes. As a result of this padding, the length of encrypted data could be greater than the original plaintext.<br />
The RC2 class provides the EffectiveKeySize property, which is used to get or set the effective key size of the RC2 secret encryption key. An exception is occured whenever size is detected. The <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rc2cryptoserviceprovider" target="_blank">RC2CryptoServiceProvider</a> class is the concrete RC2 algorithm class and it extends the RC2 class. The <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rc2cryptoserviceprovider" target="_blank">RC2CryptoServiceProvider</a> class provides the UseSalt Property. The UseSalt proeperty is set to false by deafult. However in any condition if this(usesalt) property is set to True, the encrypted value includes an 11-byte long 0 value salt.</p>
<p>Note : we need to access RC2CryptoServiceProvider class here.</p>
<pre class="brush: csharp; title: ; notranslate">
 RC2CryptoServiceProvider key = new RC2CryptoServiceProvider();
</pre>
<p>The same Function of Encryption and Decryption which we used in previous Symmetric Encryption program. Just point to be noted to change the Symmetric class here which is RC2CryptoServiceProvider for RC2 encryption.</p>
<p><span style="text-decoration:underline;"><strong><span style="color:#800000;text-decoration:underline;">How to use Encrypt Function</span></strong></span></p>
<pre class="brush: csharp; title: ; notranslate">
//Creating object of RC2 class.
RC2CryptoServiceProvider key = new RC2CryptoServiceProvider();
// Encrypt a string to a byte array.
 byte[] buffer = Encrypt(&quot;ABC&quot;, key); // ABC is an example you can put anytext as your need
</pre>
<p><span style="text-decoration:underline;"><strong><span style="color:#800000;text-decoration:underline;">How to use Decrypt Function</span></strong></span></p>
<pre class="brush: csharp; title: ; notranslate">
//Creating object of RC2 class.
RC2CryptoServiceProvider key = new RC2CryptoServiceProvider();
// You can make global variable for holding encrypted byte array value.
byte[] buffer = Encrypt(&quot;ABC&quot;, key);
string strText = Decrypt(buffer, key); // strText will hold the original text back.
</pre>
<h2><span style="color:#800000;">Rijndael Class</span></h2>
<p>The Rijndael class is an abstract class that extends the SymmetricAlgorithm class and provides support for the Rijndael algorithm. The algorithm of Rijndael encryption has been designed to replace the aging DES(Data Encryption Standard) algorithm. Like DES, it is a block cipher. It uses 128-bit, 192-bit or 256-bit keys. This implementation encrypts 128-bit blocks. (DES used 56-bit keys and 64-bit blocks.)</p>
<p>As we have read It is considered to be the replacement for the DES algorithm and was adopted as a <a href="http://en.wikipedia.org/wiki/Federal_Information_Processing_Standard" target="_blank">Federal Information Processing Standard(FIPS)</a> standard in 2001. The <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx" target="_blank">RijndaelManaged</a> class is a concrete class and it extends the Rijndael class. The Rijndael class is extended by CLR managed class, whereas the Symmetric encryption classes are extened by CSPs. The RijndaelManagedTransform class is initialized by the CreateEncryptor and CreateDecryptor methods of the RijndaelManaged class and is used in conjuction with the RijndaelMaaged class to encrypt data.<br />
In .NetFramework Rijndael algorithm supports a fixed encryption key size of 128 bits, 192 bits, 192 bits or 256 bits as <strong>9 rounds if the key/block size is 128 bits ,11 rounds if the key/block size is 192 bits and 13 rounds if the key/block size is 256 bits</strong><br />
The Rijndael class is the predecessor of the Aes algorithm. You should use the Aes algorithm instead of Rijndael.</p>
<p>Note : we need to access <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx" target="_blank">RijndaelManaged</a> class here.</p>
<pre class="brush: csharp; title: ; notranslate">
 RijndaelManaged key = new RijndaelManaged();
</pre>
<p>The same Function of Encryption and Decryption which we used in previous Symmetric Encryption program. Just point to be noted to change the Symmetric class here which is <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx" target="_blank">RijndaelManaged</a> for Rijndael encryption.</p>
<p><span style="text-decoration:underline;"><strong><span style="color:#800000;text-decoration:underline;">How to use Encrypt Function</span></strong></span></p>
<pre class="brush: csharp; title: ; notranslate">
//Creating object of RijndaelManaged class.
RijndaelManaged key = new RijndaelManaged();
// Encrypt a string to a byte array.
 byte[] buffer = Encrypt(&quot;ABC&quot;, key); // ABC is an example you can put anytext as your need
</pre>
<p><span style="text-decoration:underline;"><strong><span style="color:#800000;text-decoration:underline;">How to use Decrypt Function</span></strong></span></p>
<pre class="brush: csharp; title: ; notranslate">
//Creating object of RijndaelManaged class.
RijndaelManaged key = new RijndaelManaged();
// You can make global variable for holding encrypted byte array value.
byte[] buffer = Encrypt(&quot;ABC&quot;, key);
string strText = Decrypt(buffer, key); // strText will hold the original text back.
</pre>
<p><strong><span style="color:#800000;">Look at given below for output by using different Symmetric Algorithm applied on same plain text (eg : ABC)</span></strong></p>
<div id="attachment_734" class="wp-caption aligncenter" style="width: 633px"><a href="http://raviranjankr.files.wordpress.com/2012/08/3despic.png"><img class=" wp-image-734   " title="3DESpic" src="http://raviranjankr.files.wordpress.com/2012/08/3despic.png?w=623&#038;h=100" alt="" width="623" height="100" /></a><p class="wp-caption-text">Outcome of &#8220;ABC&#8221; text Encryption by using TripleDES algo</p></div>
<div id="attachment_735" class="wp-caption aligncenter" style="width: 633px"><a href="http://raviranjankr.files.wordpress.com/2012/08/rc2pic.png"><img class="size-full wp-image-735" title="RC2Pic" src="http://raviranjankr.files.wordpress.com/2012/08/rc2pic.png?w=640" alt=""   /></a><p class="wp-caption-text">Outcome of &#8220;ABC&#8221; text Encryption by using RC2 Algo</p></div>
<div id="attachment_736" class="wp-caption aligncenter" style="width: 644px"><a href="http://raviranjankr.files.wordpress.com/2012/08/rijndaelalgo.png"><img class="size-full wp-image-736" title="RijndaelAlgo" src="http://raviranjankr.files.wordpress.com/2012/08/rijndaelalgo.png?w=640" alt=""   /></a><p class="wp-caption-text">Outcome of &#8220;ABC&#8221; text Encryption by using Riijndael Algo</p></div>
<p><span style="text-decoration:underline;"><strong><span style="color:#800000;text-decoration:underline;">Further Read</span></strong></span><br />
<a href="http://en.wikipedia.org/wiki/Triple_DES" target="_blank">TripleDES Algo- [Wikipedia]</a><br />
<a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.tripledes.aspx" target="_blank">TripleDES class-[MSDN]</a><br />
<a href="http://en.wikipedia.org/wiki/RC2" target="_blank">RC2-[Wikipedia]</a><br />
<a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rc2.aspx" target="_blank">RC2 class-[MSDN]</a></p>
<br />Filed under: <a href='http://raviranjankr.wordpress.com/category/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/category/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/category/codeproject/'>CodeProject</a>, <a href='http://raviranjankr.wordpress.com/category/cryptography/'>Cryptography</a> Tagged: <a href='http://raviranjankr.wordpress.com/tag/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/tag/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/tag/codeproject/'>CodeProject</a>, <a href='http://raviranjankr.wordpress.com/tag/cryptography/'>Cryptography</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raviranjankr.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raviranjankr.wordpress.com/719/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=719&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raviranjankr.wordpress.com/2012/08/15/cryptography-symmetric-encryption-by-symmetric-algorithm-classes-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:thumbnail url="http://raviranjankr.files.wordpress.com/2012/08/tripledesanimpic.gif?w=150" />
		<media:content url="http://raviranjankr.files.wordpress.com/2012/08/tripledesanimpic.gif?w=150" medium="image">
			<media:title type="html">TripleDESAnimPic</media:title>
		</media:content>

		<media:content url="http://0.gravatar.com/avatar/c33505bacfac61a7a64b55a03706ca80?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raviranjankr</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/08/tripledesanimpic.gif" medium="image">
			<media:title type="html">TripleDESAnimPic</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/08/3despic.png?w=300" medium="image">
			<media:title type="html">3DESpic</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/08/rc2pic.png" medium="image">
			<media:title type="html">RC2Pic</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/08/rijndaelalgo.png" medium="image">
			<media:title type="html">RijndaelAlgo</media:title>
		</media:content>
	</item>
		<item>
		<title>Cryptography : Symmetric Encryption by Symmetric Algorithm Classes &#8211; Part 1</title>
		<link>http://raviranjankr.wordpress.com/2012/07/18/cryptography-symmetric-encryption-by-symmetric-algorithm-classes-part-1/</link>
		<comments>http://raviranjankr.wordpress.com/2012/07/18/cryptography-symmetric-encryption-by-symmetric-algorithm-classes-part-1/#comments</comments>
		<pubDate>Wed, 18 Jul 2012 09:58:43 +0000</pubDate>
		<dc:creator>Ravi Ranjan Kumar</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Cryptography]]></category>

		<guid isPermaLink="false">http://raviranjankr.wordpress.com/?p=647</guid>
		<description><![CDATA[Cryptographic : Cryptography refer to a process or an mechanism which enable us to read and write sensitive or secure data. we can find Cryptography features in all version of .Net Framework from 1.0 to 4.0. however Latest version of &#8230; <a href="http://raviranjankr.wordpress.com/2012/07/18/cryptography-symmetric-encryption-by-symmetric-algorithm-classes-part-1/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=647&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h3><strong><span style="color:#800000;">Cryptographic :</span></strong></h3>
<p>Cryptography refer to a process or an mechanism which enable us to read and write sensitive or secure data. we can find Cryptography features in all version of .Net Framework from 1.0 to 4.0. however Latest version of .Net framework providing new types and significant enhancement to existing types that can support support symmetric and asymmetric encryption and hasing efficiently. we used cryptographic types in .NET framework application to ensure secure communication and the protection of sensitive data. so just Cryptography is the one step in ensuring confidentiality of your sensitive information.</p>
<h3><span style="color:#000000;"><strong><span style="color:#800000;">Encryption</span> :</strong></span></h3>
<p>Encryption is a process of converting useful <strong>data(Plain text) into ciphertext(ciphertext is called Encrypted Text) format</strong> by using an algorithm. A cipher of higher bit strength is always considered stronger, or more secure than one of a lower bit strength. Encryption is just one of the part of Cryptography.</p>
<h3><strong><span style="color:#800000;"> Decryption :</span></strong></h3>
<p>Decryption is the process of converting back ciphertext(Encrypted text) to Plain text(Readable data). Any information that is encrypted must eventually get decrypted. while decryption we need to use same encryption key what we used for encryption. Note : Symmetric and asymmetric are the two main categories of encryption algorithm. The .NET Framework provides several classes located in the using System.Security.Cryptography namespace which implemented both the Symmetric and asymmetric encryption algorithm.</p>
<p>Have a look at below fig :</p>
<div id="attachment_650" class="wp-caption aligncenter" style="width: 610px"><a href="http://raviranjankr.files.wordpress.com/2012/07/cryptencrdecrp.gif"><img class="size-full wp-image-650 " title="Cryptography" src="http://raviranjankr.files.wordpress.com/2012/07/cryptencrdecrp.gif?w=640" alt=""   /></a><p class="wp-caption-text">Animated Pic &#8211; Encryption and Decryption</p></div>
<h3><strong><span style="color:#800000;">Symmetric Encryption :</span></strong></h3>
<p>It is also called Single Key, private Key or <strong>Secret Key Cryptography(SKC)</strong>.while using Symmetric Encryption <span style="color:#800000;">only a single key is used for both encryption and decryption process.</span></p>
<div id="attachment_673" class="wp-caption aligncenter" style="width: 610px"><a href="http://raviranjankr.files.wordpress.com/2012/07/symmetricalgo.gif"><img class="size-full wp-image-673" title="SymmetricAlgo" src="http://raviranjankr.files.wordpress.com/2012/07/symmetricalgo.gif?w=640" alt=""   /></a><p class="wp-caption-text">Animated Pic:- Symmetric Encryption Decryption Concept</p></div>
<p>Symmetric encryption algorithm are always considered as less complex than asymmetric algorithm because only single key is being used in Symmetric encryption.</p>
<h3><strong><span style="color:#800000;">Symmetric Algorithm classes :</span></strong></h3>
<pre class="brush: plain; title: ; notranslate">System.Security.Cryptography </pre>
<p>namespace provides encryption classes that provide most popular symmetric algorithms like</p>
<ul>
<li>Data Encryption Standard (DES)</li>
<li>Triple Data Encryption Standard</li>
<li>(Rivest Cipher) RC2</li>
<li>Rijndael</li>
<li>Advanced Encryption Standard (AES)  [Supported in NET Framework 3.5 and 4.0 only ]</li>
</ul>
<p>The symmetric encryption classes a common base class named the SymmetricAlgorithm class. As a base class, the SymmetricAlgorithm class is abastract(MustInherit) and provides functionality intrinsic in all extended symmetric encryption classes. The <span style="color:#800000;">DES</span>, <span style="color:#800000;">TripleDES</span>, <span style="color:#800000;">RC2</span> and <span style="color:#800000;">Rijndael classes</span> extend the SymmetricAlgorithm classes and also provide symmetric encryption in which each and every of these classes is further extended by its correspoding <span style="color:#800000;">CryptoServiceProvider(CSP)</span> class. CSP class are kind of concrete class that gives service of Cryptography by wrapping unmanaged object that are external to <span style="color:#800000;">Common Language Runtime(CLR)</span> Lets see One by One Implementation DES Class : The <span style="color:#800000;">Data Encryption Standard(DES)</span> class is an abstract(MustInherit) class that extends the SymmetricAlgorithm class and also provides support for its algorithm. The <span style="color:#800000;">DESCryptoServiceProvider</span> class extends the DES Class. however The .Net Framework supporing DES algorith because of its huges popularity. It is generally most widely used secret key system, particularly in securing financial data. actually It was originally developed to be embedded in hardware like Automated Teller Machines (ATMs) typically use DES.</p>
<h3><span style="text-decoration:underline;"><strong><span style="color:#800000;text-decoration:underline;">Implementation of the DES class</span></strong></span></h3>
<p>Data Encryption Standard(DES) symmetric key or private-key encryption, work as the sender and receiver of a message share a single or common key that is used to encrypt and decrypt the message. The key is a string of digits that generated by a complex mathematical algorithm, or by formula.</p>
<p>The following code sample guide you how to use and implement the <span style="color:#800000;"><a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.descryptoserviceprovider.aspx">DESCryptoServiceProvide</a>r</span> class.</p>
<p>To perform Encryption and Decryption. you must add</p>
<pre class="brush: csharp; title: ; notranslate">
using System.Security.Cryptography; // Namespace
</pre>
<p>Now take a look at encryption function</p>
<pre class="brush: csharp; title: ; notranslate">
public static byte[] Encrypt(string strText, SymmetricAlgorithm key)
{
// Create a memory stream.
MemoryStream ms = new MemoryStream();

// Create a CryptoStream using the memory stream and the
// CSP(cryptoserviceprovider) DES key.
CryptoStream crypstream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);

// Create a StreamWriter to write a string to the stream.
StreamWriter sw = new StreamWriter(crypstream);

// Write the strText to the stream.
sw.WriteLine(strText);

// Close the StreamWriter and CryptoStream.
sw.Close();
crypstream.Close();

// Get an array of bytes that represents the memory stream.
byte[] buffer = ms.ToArray();

// Close the memory stream.
ms.Close();

// Return the encrypted byte array.
return buffer;
}
</pre>
<p>In above code, <span style="color:#800000;">CreateEncryptor()</span> function is used to encryptor object with the current <span style="color:#800000;"><a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.key.aspx"><span style="color:#800000;">Key</span></a></span> property and initialization vector.</p>
<p>Now in same way we need to create function for Decrypt the PlainText(Encrypted Text)<br />
have a look at given function which is responsible to decrypt encrypted text.</p>
<pre class="brush: csharp; title: ; notranslate">
public static string Decrypt(byte[] encryptText, SymmetricAlgorithm key)
        {
            // Create a memory stream to the passed buffer.
            MemoryStream ms = new MemoryStream(encryptText);
 // Create a CryptoStream using  memory stream and CSP DES key.
            CryptoStream crypstream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);

            // Create a StreamReader for reading the stream.
            StreamReader sr = new StreamReader(crypstream);

            // Read the stream as a string.
            string val = sr.ReadLine();

            // Close the streams.
            sr.Close();
            crypstream.Close();
            ms.Close();

            return val;
        }
</pre>
<p>In above code, decryption is handled in same way by using <span style="color:#800000;">CreateDecryptor()</span> function instead of <span style="color:#800000;">CreateEncryptor()</span></p>
<p>Now we have created function, so we can use both function to appropriate manner to accomplishment of Encryption Decryption task.</p>
<p><span style="text-decoration:underline;"><strong>How to use Encrypt Function</strong></span></p>
<pre class="brush: csharp; title: ; notranslate">
 DESCryptoServiceProvider key = new DESCryptoServiceProvider();
 // Encrypt a string to a byte array.
  byte[] buffer = Encrypt(&quot;ABC&quot;, key); // ABC is an example you can put anytext as your need
</pre>
<p><span style="text-decoration:underline;"><strong>How to use Decrypt Function</strong></span></p>
<pre class="brush: csharp; title: ; notranslate">
DESCryptoServiceProvider key = new DESCryptoServiceProvider();
// You can make global variable for holding encrypted byte array value.
byte[] buffer = Encrypt(&quot;ABC&quot;, key);
string strText = Decrypt(buffer, key);
</pre>
<h3><strong><span style="color:#800000;">Download Source Code :</span></strong></h3>
<p>Want to Download Source Code : <a href="http://dl.dropbox.com/u/27553051/SourceCodes/Cryptography-SymmetricDES.zip" target="_blank">Click Me!</a></p>
<h3><strong><span style="text-decoration:underline;"><span style="color:#800000;text-decoration:underline;">References :</span></span></strong></h3>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Cryptography" target="_blank">Cryptography &#8211; Wikipedia</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.descryptoserviceprovider.aspx" target="_blank">DESCryptoServiceprovider class -MSDN</a></li>
<li><a href="http://www.garykessler.net/library/crypto.html" target="_blank">Overview of Cryptography -garrykessler</a></li>
</ul>
<p><strong><span style="text-decoration:underline;color:#800000;">Coming Next </span></strong></p>
<p>In the next article, hopefully I will come back with other symmetric algorithm implementation like <span style="color:#800000;">AES, RC2, Rijndael</span> and <span style="color:#800000;">TripleDES <span style="color:#000000;">for beginners. </span></span></p>
<br />Filed under: <a href='http://raviranjankr.wordpress.com/category/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/category/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/category/codeproject/'>CodeProject</a>, <a href='http://raviranjankr.wordpress.com/category/cryptography/'>Cryptography</a> Tagged: <a href='http://raviranjankr.wordpress.com/tag/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/tag/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/tag/codeproject/'>CodeProject</a>, <a href='http://raviranjankr.wordpress.com/tag/cryptography/'>Cryptography</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raviranjankr.wordpress.com/647/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raviranjankr.wordpress.com/647/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=647&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raviranjankr.wordpress.com/2012/07/18/cryptography-symmetric-encryption-by-symmetric-algorithm-classes-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:thumbnail url="http://raviranjankr.files.wordpress.com/2012/07/cryptencrdecrp.gif?w=150" />
		<media:content url="http://raviranjankr.files.wordpress.com/2012/07/cryptencrdecrp.gif?w=150" medium="image">
			<media:title type="html">Cryptencrdecrp</media:title>
		</media:content>

		<media:content url="http://0.gravatar.com/avatar/c33505bacfac61a7a64b55a03706ca80?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raviranjankr</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/07/cryptencrdecrp.gif" medium="image">
			<media:title type="html">Cryptography</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/07/symmetricalgo.gif" medium="image">
			<media:title type="html">SymmetricAlgo</media:title>
		</media:content>
	</item>
		<item>
		<title>A Basic Introduction of Class For Beginners.</title>
		<link>http://raviranjankr.wordpress.com/2012/02/24/a-basic-introduction-of-class-for-beginners/</link>
		<comments>http://raviranjankr.wordpress.com/2012/02/24/a-basic-introduction-of-class-for-beginners/#comments</comments>
		<pubDate>Fri, 24 Feb 2012 14:50:14 +0000</pubDate>
		<dc:creator>Ravi Ranjan Kumar</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[class keyword]]></category>
		<category><![CDATA[object oriented language]]></category>

		<guid isPermaLink="false">http://raviranjankr.wordpress.com/?p=249</guid>
		<description><![CDATA[Introduction As we know C# is a true object-oriented language, therefore everything must be placed inside a class. Basically a class is a user defined data type with a template that serves to define its properties. In short class is &#8230; <a href="http://raviranjankr.wordpress.com/2012/02/24/a-basic-introduction-of-class-for-beginners/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=249&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>Introduction</strong><br />
As we know C# is a true object-oriented language, therefore everything must be placed inside a class. Basically a class is a user defined data type with a template that serves to define its properties. In short class is nothing it&#8217;s just a kind of template for object (as what an object look like and how it behaves) which we create in program, a kind of specification or blue print which must be followed by object created for correspondence class.</p>
<pre class="brush: csharp; title: ; notranslate">
class classname
{
//here define member of class
[variable declaration;]
[methods declarations;]
}
</pre>
<p>While creating class the first step is used which is declaring class keyword and then name of class or identifier. After declaring class name the next step is declaring body of class which will start with opening curly braces <span style="color:#800000;">{</span> and also end with closing curly braces<span style="color:#800000;"> }</span>.<br />
We need to declare body inside opening and closing curly braces.</p>
<p><strong>class program { }</strong></p>
<p>Here class is a keyword and program is valid C# identifier. Class and structs are very similar but class is a reference type. Also classes include more features compared to structs. Have a look at some categories of class member.</p>
<div class="wp-caption aligncenter" style="width: 458px"><img src="http://raviranjankr.files.wordpress.com/2012/02/imgclasscateg.jpg?w=448&#038;h=268" alt="" width="448" height="268" /><p class="wp-caption-text">Class Members</p></div>
<p><strong>Note :-</strong>  C# classes are reference types, that&#8217;s why the allocation of class doesn&#8217;t exist in Stack class are always allocated on the Managed Heap. The Managed Heap is an area of memory that is managed by the <span style="color:#800000;"><strong>CLR</strong></span>( Common Language Runtime), which has the ability to free unused memory blocks (objects) in a process known as Garbage Collection.</p>
<p><strong>The Class Name :-</strong> The class keyword is followed by the name of the class keyword. in the given code Program is the class. While creating class we must consider naming convention and rule.</p>
<p><span style="text-decoration:underline;"><strong>Class Naming Conventions</strong></span><br />
Class names should follow certain naming conventions or guidelines.</p>
<p>A class name :</p>
<ul>
<li>Should be meaningful(strongly recommended)</li>
<li>Should ideally be a noun.</li>
<li>Can use either the Pascal case or Camel Case.</li>
</ul>
<p>Rules for Naming classes in C# Name of Classes :</p>
<ul>
<li>Must begin with a letter. This letter may be followed by a sequence of letters, digits <span style="color:#800000;">(0-9)</span> or <span style="color:#800000;">&#8220;-&#8221;</span>.</li>
<li>The first character in a class name cannot be a digit.</li>
<li>Must not contain any embedded space or symbol like <span style="color:#800000;">?</span>-<span style="color:#800000;">+!@$%^&amp;*(){}[],;&#8221;&#8216;/</span> and <span style="color:#800000;">\</span> however an underscore <span style="color:#800000;">(&#8216;_&#8217;)</span> can be used wherever a space is required.</li>
<li>Must not use a keyword for a class name. For example you can&#8217;t declare a class called public.</li>
</ul>
<p>For more reference, Have a read<br />
<a title="Names of Classes" href="http://msdn.microsoft.com/en-us/library/ms229040.aspx">MSDN-[ Names of Classes ]</a><br />
<a title="Naming Guidelines" href="http://msdn.microsoft.com/en-us/library/xzf533w0(v=vs.71).aspx">MSDN-[ Naming Guidelines ]</a><br />
<a title="Naming Conventions for C#" href="http://msmvps.com/blogs/paulomorgado/archive/2007/05/20/naming-conventions-for-c.aspx">Naming Conventions for C#</a></p>
<p>A example of class structure in Program</p>
<div id="attachment_261" class="wp-caption aligncenter" style="width: 510px"><a href="http://raviranjankr.files.wordpress.com/2012/02/imgclassstrctr.png"><img class=" wp-image-261  " title="Imgclassstrctr" src="http://raviranjankr.files.wordpress.com/2012/02/imgclassstrctr.png?w=500&#038;h=300" alt="" width="500" height="300" /></a><p class="wp-caption-text">Example of Class Structure</p></div>
<p><strong>The Using Keyword</strong><br />
First statement in a typical C# program is using System; the using keyword is used to include the namespace in the program. Keywords are reserved words that have a special meaning, The statement using system, declares that you can refer to the class defined in the namespace without using the fully qualified name. A program can include multiple using statements.</p>
<p>For more reference. have a read<br />
<a title="using (C# Reference)" href="http://msdn.microsoft.com/en-us/library/zhdeatwt.aspx">MSDN-[ using (C# Reference) ]</a><br />
<a title="The " href="http://www.codeproject.com/Articles/17106/The-using-Keyword-in-C">The &#8220;using&#8221; Keyword in C#</a></p>
<p><strong>The namespace Keyword </strong><br />
The namespace keyword is used to declare a namespace in a program. Generally namespace is a collection of class. A program can contain multiple namespace.</p>
<p>For more reference, have a read<br />
<a title="Namespaces (C# Programming Guide)" href="http://msdn.microsoft.com/en-us/library/0d941h9d.aspx">MSDN-[ Namespace ]</a></p>
<p><strong>The class Keyword</strong><br />
The class Keyword is used to declare a class.</p>
<p>For more reference, Have a look at<br />
<a title="class (C# Reference)" href="http://msdn.microsoft.com/en-us/library/0b0thckt.aspx">MSDN-[ class (C# Reference) ]</a></p>
<p><strong>The Comment Entry</strong><br />
Comment are a part of the program and are used to explain code. Compilers ignore comment entries.</p>
<p>for more reference, have a read<br />
<a title="Adding Comments in C# Code" href="http://msdn.microsoft.com/en-us/library/tkxs89c5(v=vs.71).aspx">MSDN-[ Adding Comments in C# Code ]</a></p>
<p><strong>Member Variables</strong><br />
Variables are used to store data. Variables are also called the data members of a class.</p>
<p>for more reference, have a read<br />
<a title="Variables" href="http://msdn.microsoft.com/en-us/library/aa691160(v=vs.71).aspx">MSDN-[ Variables ]</a></p>
<p><strong>Member Functions</strong><br />
A function is a set of statement that perform a specific task in response to a message. The function of a class is called member function in C#.</p>
<p>for more reference, have a read<br />
<a title="Methods (C# Programming Guide)" href="http://msdn.microsoft.com/en-us/library/ms173114.aspx">MSDN-[ Methods (C# Programming Guide) ]</a></p>
<p><strong>Constructor :- </strong><br />
A constructor is a special type of method which call itself when you create a new instance of a class. The name of a constructor is the same as the name of the class that contain it.<br />
<a title="Constructors (C# Programming Guide)" href="http://msdn.microsoft.com/en-us/library/ace5hbzh.aspx">MSDN-[Constructors (C# Programming Guide)]</a><br />
<a title="Constructor" href="http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29">Constructor in OOPL</a></p>
<p><span style="text-decoration:underline;"><strong>Classes and Objects :- </strong></span><br />
Classes are like the heart of an <span style="color:#800000;">OOPL</span>(object Oriented Programming Language). we need to use it even we write simplest of program  where objects of class provides the benefit of modularity and information hiding where as class provides the benefit of reusuability. we programmer used the same class same code and over again to create many objects.<br />
The class is declared with the <span style="color:#800000;">Main()</span> Method and it got initiated in same class. The First line of code that a C# compiler looks in the source file compiled is the <span style="color:#800000;">Main()</span> function. To use a class the members of a class, you need to create an object of the class. Objects interact with each other by passing messages and by responding to the received messages. <em>Objects use methods to pass messages.</em> In C# the task of passing messages can be accomplished by using member functions. All the object of a class share the same copy of the member function but they maintain a separate copy of the number variables in memory. The sharing of member function and non sharing of member variables of classes and object in shown in the following figure :</p>
<div class="wp-caption aligncenter" style="width: 413px"><img class=" " src="http://raviranjankr.files.wordpress.com/2012/02/imgclassobject.jpg?w=403&#038;h=336" alt="" width="403" height="336" /><p class="wp-caption-text">Objects of Class</p></div>
<p><strong>What is Object :-</strong><br />
A object  is refer as an instance of an executable copy of class or an object is a software bundle of variables and related  methods where object maintain its state in variable and implement its behavior with methods.</p>
<p><img class="aligncenter" src="http://raviranjankr.files.wordpress.com/2012/02/picture1.jpg?w=448&#038;h=251" alt="" width="448" height="251" /></p>
<p><strong>How to create Object :- </strong><br />
Let take a look at below animated image where a class is laptop would declare the instance variable to contain the details of webcam, the screen size, Brand,Color, Ram and corresponding processor. The class also can contain some appropriate methods.<br />
After we’ve created the Laptop class, we can create any number of Laptop objects from the class. When we create an instance of a class, the system allocates enough memory for the object and all its instance variables. Each instance gets its own copy of all the instance variables defined in the class.<br />
In addition to instance variables, classes can define class variables. A class variable contains information that is shared by all instances of the class.<br />
Take a look at below image, Where I tried to clear concept by using two object objcet1 and object2 where each containing its own details like object1 has Intel I3 Processor whereas object2 has AMD processor</p>
<div id="attachment_247" class="wp-caption aligncenter" style="width: 510px"><a href="http://raviranjankr.files.wordpress.com/2012/02/classobject.gif"><img class=" wp-image-247" title="classobject" src="http://raviranjankr.files.wordpress.com/2012/02/classobject.gif?w=500&#038;h=300" alt="" width="500" height="300" /></a><p class="wp-caption-text">Animated Image - Example of class</p></div>
<p><strong>Illustration with codes </strong></p>
<div id="attachment_250" class="wp-caption aligncenter" style="width: 510px"><a href="http://raviranjankr.files.wordpress.com/2012/02/classandobject.jpg"><img class=" wp-image-250" title="ClassandObject" src="http://raviranjankr.files.wordpress.com/2012/02/classandobject.jpg?w=500&#038;h=300" alt="" width="500" height="300" /></a><p class="wp-caption-text">Creating Object of Class</p></div>
<p>While we create a class, we used to create a data type which is just a declaration. We can create object of this data type. Creating object is a two-step process, which is given below:</p>
<ul>
<li>First declare a variable of type class. When we do so we are actually creating a reference that will refer to an object. In the corresponding slide this is done in <span style="color:#800000;">Main( )</span> through the following statement:
<pre class="brush: csharp; title: ; notranslate">
Program obj;
</pre>
</li>
<li>In Second step, allocate memory for an object and assign it to the reference. Allocating memory for an object is done using the new operator.  new dynamically allocates memory for an object and returns its reference. So, every object is referred to by a reference only. In the corresponding slide we have created an object of the Program class through the statement:
<pre class="brush: csharp; title: ; notranslate">
 obj = new Program( ) ;
</pre>
</li>
</ul>
<p>When the above statement is executed, an object gets created in memory, as you can see in the figure, which is referred to by obj.</p>
<p>Using obj we have called the <span style="color:#800000;">setdata( )</span> and <span style="color:#800000;">showdata( )</span> member functions of the Program class. In the <span style="color:#800000;">setdata( )</span> function we have stored some values in the data members name and age. In the <span style="color:#800000;">showdata( )</span> function we have displayed the values.</p>
<p>Hence, we can say that</p>
<pre class="brush: csharp; title: ; notranslate">
// Creating Instance of Class
Program obj = new Program();
</pre>
<ul>
<li><strong>Declaration</strong>: <span style="color:#800000;">Program obj</span> is a variable declaration that declares to the compiler that the name obj will be used to refer to a Program object. Notice that a class name is used as the variable&#8217;s type.</li>
<li><strong>Instantiation</strong>: <span style="color:#800000;">new</span> is a operator that creates the new object (allocates space for it)</li>
<li><strong>Initialization</strong>: <span style="color:#800000;">Program()</span> is a call to Program&#8217;s constructor, which initializes the object.</li>
</ul>
<p>here,  Declaration doesn&#8217;t create a new object of class,<br />
<span style="color:#800000;">Program obj</span> doesn&#8217;t create new object of Program class. Just a variable name obj to holds a Program object.<br />
<strong>Note : -</strong> To create class object or any other object, we need to use the <span style="color:#800000;">new</span> operator<br />
The member function of the class are accessed through an object of the class by using the <span style="color:#800000;">&#8220;.&#8221;</span> Operator. As shown below</p>
<div id="attachment_251" class="wp-caption aligncenter" style="width: 510px"><a href="http://raviranjankr.files.wordpress.com/2012/02/multiclassobject.gif"><img class=" wp-image-251" title="Multiclassobject" src="http://raviranjankr.files.wordpress.com/2012/02/multiclassobject.gif?w=500&#038;h=300" alt="" width="500" height="300" /></a><p class="wp-caption-text">Animated Image - Multiple Object of Class</p></div>
<p>In the above figure we can see that Program have two objects of the class Program, which are referred by<span style="color:#800000;"> obj1</span> and <span style="color:#800000;">obj2</span> . We have followed the same way to create these objects as previous program. i.e first declaring the references and then assigning to them the address of objects created using new.<br />
Both the objects have their own copies of data members.</p>
<p><span style="text-decoration:underline;"><strong>Constructor :-</strong></span><br />
A constructor is a special member function of a class, which gets called automatically when an object is created. Hence, a constructor is used to set up values into an object. A constructor has the same name as the class.<br />
It would become tedious to call a separate function every time to assign initial values to the objects. What if we would be able to initialize the objects when they are created? This can be done using constructors</p>
<pre class="brush: csharp; title: ; notranslate">
class Program
    {
        string name;
        int age;
        Program(string n,int a)
        {
            name = n;  // assigning value to name
            age = a;  // assigning value to age
        }
        public void showdata()
        {
            Console.WriteLine(name+&quot; &quot;+age);
        }
}
</pre>
<p>Here, as soon as the statement, <span style="color:#800000;">new Program( )</span>, gets executed the constructor of Program class gets called. In the constructor we have assigned values to the data members. The values are displayed using the <span style="color:#800000;">showdata( )</span> function. The only problem is that all the objects of the Program class would get initialised with the same values We would not be able to assign different values to the objects.<br />
To overcome this problem we would need a constructor to which we can pass values as we did in the <span style="color:#800000;">setdata( )</span> function.</p>
<pre class="brush: csharp; title: ; notranslate">
static void Main(String[] args)
       {
           Program obj = new Program(&quot;Ram&quot;, 20);
           obj.showdata();
        }
</pre>
<p>have a look at given image to know how value pass between Member variables using Constructor.</p>
<p style="text-align:center;"><a href="http://raviranjankr.files.wordpress.com/2012/02/imgclassconstruct.png"><img class="aligncenter  wp-image-269" title="ImgClassconstruct" src="http://raviranjankr.files.wordpress.com/2012/02/imgclassconstruct.png?w=600&#038;h=400" alt="" width="600" height="400" /></a></p>
<p><strong>Classes and its Accessibility </strong></p>
<p><strong></strong>while defining class, each class need to specify its level of visibility. class visibility is used to decide which parts of the system can create class objects.<br />
A class can have one of the two visibility modifiers : <span style="color:#800000;">public</span> or <span style="color:#800000;">internal</span>.<br />
<strong>Note :-</strong> if we don&#8217;t explicitly mention visibility modifier of a class, its implicitly set to <span style="color:#800000;">&#8216;internal&#8217;</span>. 1.e by default all classes are internal.</p>
<div id="attachment_279" class="wp-caption aligncenter" style="width: 410px"><a href="http://raviranjankr.files.wordpress.com/2012/02/imgclassmod.jpg"><img class=" wp-image-279  " title="Imgclassmod" src="http://raviranjankr.files.wordpress.com/2012/02/imgclassmod.jpg?w=400&#038;h=200" alt="" width="400" height="200" /></a><p class="wp-caption-text">Accessibility of class members</p></div>
<p><span style="text-decoration:underline;"><strong>abstract :</strong></span> An abstract class is same as others classes but its marks with abstract keyword. abstract class can have normal properties, constructor and other methods.<br />
An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.<br />
Abstract class is the way to make sure that its closed subclass must override abstract method.<br />
In sense, if you want our subclass have which method to overridden, we have to make that methods and the superclass to be abstract.</p>
<p>Note :- we can&#8217;t create an instance of abstract class by using <span style="color:#800000;">new</span> keyword. <em>if you will try,it will give compile time error</em>.</p>
<p><strong>Some Point :-</strong><br />
why we can&#8217;t I create an abstract constructor on an abstract C# class.<br />
check <a href="http://stackoverflow.com/questions/504977/why-cant-i-create-an-abstract-constructor-on-an-abstract-c-sharp-class">answer</a> because abstract means you must override it in any non-abstract child class and you cannot override a constructor.<br />
for more reference, have a look at<br />
<a title="abstract (C# Reference)" href="http://msdn.microsoft.com/en-us/library/sf985hc5.aspx">MSDN-[ abstract class ] </a><br />
<a title="Abstract Class" href="http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface">Abstract Class</a><br />
<a title="All about abstract classes." href="http://www.codeproject.com/KB/cs/jmabstractclasses.aspx?q=abstract+class+in+C%23">All about abstract classes.</a></p>
<p><span style="text-decoration:underline;"><strong>sealed</strong></span><strong> :</strong> A sealed class is is declared with sealed keyword, The sealed modifier can be applied to class, instances methods and properties.<br />
A sealed class cannot be used as a base class or sub classed. it means a sealed class cannot be inherited by anyway. Sealed classes are primarily used to prevent derivation</p>
<pre class="brush: csharp; title: ; notranslate">
 sealed class A
   {
        // member function
   }
  class B : A //Error : Class B can't derive from sealed type class A
   {
   }
</pre>
<p>here Child class can be sealed but when child class B try to act as parent class it will give same error. as</p>
<pre class="brush: csharp; title: ; notranslate">
 sealed class A
   {        // member function
   }
  class B : A
   {  }
 class C:B //Error
  { }
</pre>
<p>Question in Mind :- <a href="http://stackoverflow.com/questions/2134/do-sealed-classes-really-offer-performance-benefits">Do sealed classes really offer performance Benefits?</a> [Click link to get answer ]</p>
<p>for more reference, must see<br />
<a title="sealed (C# Reference)" href="http://msdn.microsoft.com/en-us/library/88c54tsw(v=vs.80).aspx">MSDN-[ sealed (C# Reference) ]</a></p>
<p><span style="text-decoration:underline;"><strong>singleton class :</strong></span> singleton class is used to ensure that only one instance of a class is can be created<br />
for more reference, <a title="Implementing Singleton in C#" href="http://msdn.microsoft.com/en-us/library/ff650316.aspx">Implementing Singleton in C#</a><br />
<a href="http://www.codeproject.com/Articles/28309/Design-pattern-FAQ-Part-1-Training#Canyouexplainsingletonpattern">explain singleton pattern</a><br />
<a href="http://msdn.microsoft.com/en-us/library/ee817670.aspx">Exploring the Singleton Design Pattern</a></p>
<p><span style="text-decoration:underline;"><strong>Generic class :</strong></span> Generic classes is generally used to encapsulate operations that are not specific to a particular data type. The most common use of generics is to create collection classes.<br />
for more reference, <a title="Generic Classes" href="http://msdn.microsoft.com/en-us/library/sz6zd40f(v=vs.80).aspx">Generic Classes</a></p>
<p><span style="text-decoration:underline;"><strong>Interface :</strong></span><br />
An Interface is a logical group of related methods with empty bodies. it can contain one or more methods,properties, indexers and events but none of them are implemented in the interface in it self. Its the responsibility of class that implement the interface to define the code for implementation of these members.</p>
<pre class="brush: csharp; title: ; notranslate">
interface InterfaceName
{
// Member declaration
//Properties Indexer and events declarations
}
</pre>
<p>here, Interface is the keyword and InterfaceName is the valid identifier (same as Class Name ) where member declaration contain only a list of members without any implementation of codes.<br />
example</p>
<pre class="brush: csharp; title: ; notranslate">
interface Iface
{
void setdata(); // Note semicolon here
}
</pre>
<p>Fore more reference, have a read<br />
<a href="http://msdn.microsoft.com/en-us/library/ms173156.aspx">MSDN-[Interfaces]</a><br />
<a href="http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface">Abstract Class versus Interface</a></p>
<p><span style="text-decoration:underline;"><strong>Inheritance :-</strong></span><br />
Inheritance represents a kind of relationship between two classes. The mechanism of designing or constructing one class from another is called inheritance.<br />
Related to Inheritance is an equally feature known as polymorphism. this feature permists the same method name to be used for different operations in different derived classes.<br />
There are some various kind of Inheritance. they include</p>
<ul>
<li>Single Inheritance (only one base class ).</li>
<li>Hierarchical Inheritance ( one base class, Many subclasses ).</li>
<li>Multilevel Inheritance (derive from a derived class ).</li>
</ul>
<p><strong>Note :-</strong> C# doesn&#8217;t support Multiple Inheritance.<br />
<a href="http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/85562.aspx">Why doesn&#8217;t C# support multiple inheritance?</a></p>
<p>You can use <a href="http://www.codeproject.com/Articles/18743/Interfaces-in-C-For-Beginners">Interface</a> to support Multiple Inheritance in your Program.<br />
<strong>Defining a subclass :</strong><br />
A subclass is defined as follows</p>
<pre class="brush: csharp; title: ; notranslate">
class subclass-name : baseclass-name
{
variables declaration;
methods declaration;
}
</pre>
<p>Here, you can see that the desecration is very similar to a normal class definition except for the use of <strong>colon <span style="color:#800000;">:</span></strong> and<span style="color:#800000;"> baseclass-name</span>. the colon signifies that the properties of the baseclass are extended to the <span style="color:#800000;">subclass-name</span>. while implemented the subclass will contain its own members as well those of the baseclass.</p>
<pre class="brush: csharp; title: ; notranslate">
class Animal{
void Disp_animal(){
Console.WriteLine(&quot;Hello I am Animal Class&quot;);
}
}
class Lion:Amimal
{
void Disp_lion(){
Console.WriteLine(&quot;Hello I am derived lion class&quot;&quot;);
}
}
</pre>
<p>while excessive each function of corresponding class</p>
<pre class="brush: csharp; title: ; notranslate">
Lion obj = new Lion();
obj.Disp_animal();// object of child class accessing parent class function.
obj.Disp_lion();
</pre>
<p>Some features of inheritance :</p>
<ul>
<li>constructor and destructors are not inherited.</li>
<li>A derived class extends its direct base class. it can&#8217;t change or remove the definitions of an inherited member.</li>
<li>An instance of a class contain a copy of all instance fields declared in the class and its base classes.</li>
<li>A derived class can hide an inherited member.</li>
<li>A derived class can override an inherited member.</li>
</ul>
<p>For more reference, have a read<br />
<a href="http://www.blackwasp.co.uk/Inheritance.aspx">What is Inheritance?</a></p>
<p><span style="text-decoration:underline;"><strong>What you got</strong></span><br />
You should have a general understanding or a feeling for the following:</p>
<ul>
<li>What is class</li>
<li>How to create an object from a class</li>
<li>What constructors are</li>
<li>What the code for a class looks like</li>
<li>What member variables are</li>
<li>How to initialize objects</li>
<li>basic Idea of Inheritance</li>
</ul>
<br />Filed under: <a href='http://raviranjankr.wordpress.com/category/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/category/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/category/codeproject/'>CodeProject</a> Tagged: <a href='http://raviranjankr.wordpress.com/tag/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/tag/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/tag/class-keyword/'>class keyword</a>, <a href='http://raviranjankr.wordpress.com/tag/codeproject/'>CodeProject</a>, <a href='http://raviranjankr.wordpress.com/tag/object-oriented-language/'>object oriented language</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raviranjankr.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raviranjankr.wordpress.com/249/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=249&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raviranjankr.wordpress.com/2012/02/24/a-basic-introduction-of-class-for-beginners/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:thumbnail url="http://raviranjankr.files.wordpress.com/2012/02/imgclassstrctr.png?w=150" />
		<media:content url="http://raviranjankr.files.wordpress.com/2012/02/imgclassstrctr.png?w=150" medium="image">
			<media:title type="html">Imgclassstrctr</media:title>
		</media:content>

		<media:content url="http://0.gravatar.com/avatar/c33505bacfac61a7a64b55a03706ca80?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raviranjankr</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/02/imgclasscateg.jpg" medium="image" />

		<media:content url="http://raviranjankr.files.wordpress.com/2012/02/imgclassstrctr.png?w=300" medium="image">
			<media:title type="html">Imgclassstrctr</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/02/imgclassobject.jpg" medium="image" />

		<media:content url="http://raviranjankr.files.wordpress.com/2012/02/picture1.jpg" medium="image" />

		<media:content url="http://raviranjankr.files.wordpress.com/2012/02/classobject.gif?w=300" medium="image">
			<media:title type="html">classobject</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/02/classandobject.jpg?w=300" medium="image">
			<media:title type="html">ClassandObject</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/02/multiclassobject.gif?w=300" medium="image">
			<media:title type="html">Multiclassobject</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/02/imgclassconstruct.png?w=300" medium="image">
			<media:title type="html">ImgClassconstruct</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2012/02/imgclassmod.jpg?w=300" medium="image">
			<media:title type="html">Imgclassmod</media:title>
		</media:content>
	</item>
		<item>
		<title>Basic Introduction of Code Access Security For Beginners</title>
		<link>http://raviranjankr.wordpress.com/2011/11/05/basic-introduction-of-code-access-security-for-beginners/</link>
		<comments>http://raviranjankr.wordpress.com/2011/11/05/basic-introduction-of-code-access-security-for-beginners/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 16:14:28 +0000</pubDate>
		<dc:creator>Ravi Ranjan Kumar</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[CodeProject]]></category>

		<guid isPermaLink="false">http://raviranjankr.wordpress.com/?p=178</guid>
		<description><![CDATA[Introduction Its a simple theoretical blog enable you to know about what is Code access security with its pron and Cons Code access security is the central security scheme in the .Net framework, used automatically by the runtime. it helps you restrict access to &#8230; <a href="http://raviranjankr.wordpress.com/2011/11/05/basic-introduction-of-code-access-security-for-beginners/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=178&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><span style="text-decoration:underline;"><strong>Introduction</strong></span><br />
Its a simple theoretical blog enable you to know about what is Code access security with its pron and Cons<br />
Code access security is the central security scheme in the .Net framework, used automatically by the runtime. it helps you restrict access to import or sensitive operation and resources. To lock system and protect them against malicious code. you need to provide least privilege, which gives user and code the freedom absolutely necessary to perform their task. code access security helps you achieve this principle of least privilege.</p>
<p><span style="text-decoration:underline;"><strong>Benefit of Code access security</strong></span><br />
In the past, role based security system relied on permission granted to the user. The .Net framework&#8217;s code access security mechanism help grant access permission to code.<br />
Many object such as file, directories and registry key in the windows operating system are controlled by access list that specify which users should have access and the kind of access they are allowed. User indentity is therefore important in evaluating access permission for the resources code. However, sometimes a trusted user may need to run code for undertain origin such as an application available from an external web site. In such cases role based security does little to protect your computer against malicious code. Evidence-based security, one the other hand can help identify suspected programs and can limit their ability to do damage.</p>
<p><span style="text-decoration:underline;"><strong>Important Feature of Code access security</strong></span><br />
An important feature of code access security is that it can function reliability only when ode is verifiably type safe. If code is not type safe. It can breach security check by making calls to the restricted areas of memory.<br />
<strong>For example :-</strong> its not legal to write type safe code that directly accesses the private member variables of an object. However it is safe. The CLR verifies that code is type safe and by default allows non type safe code to run only if it originates from the local omputer. Its is also possible to set a security policy that restricts the execution of any code is not type safe.<br />
Another limitation of code access security is that it does not protect the user who downloads an application from the Internet and runs it locally. The defaultpoilicy of local application is to grant full trust so that such an application is free to access any of the user resources.</p>
<p><span style="text-decoration:underline;"><strong>Specifying Security permission in code</strong></span><br />
There are two ways of specifying security permission in your code :<br />
<strong>a) Imperative syntax :</strong> Requirs explicitly created permission object that are used to make security checks at run time.<br />
<strong>b) Declarative syntax:</strong> Uses code attributes to specify the permission that are required to execute an assembly, a particular class, or particular methods in a class. Rules in a security policy<br />
Security policies can be configured by using the Microsoft .Net framework 2.0 Configuration utility. Policies consist of rules that can be specified at three different levels: Enterprise, Machine and User.<br />
<em>At the Enterprise level,</em> you can set blanket rule across an entire company, department, or group of computers. Machine and user level allow you to specify security rule that restrict permissions for individual computer and users.</p>
<p><span style="text-decoration:underline;"><strong>Primary entities in the code access security architecture</strong></span></p>
<p>The following are the entities envolved in the code access security architecture<br />
<strong>a) Assembly :</strong> Consist of a single file or a package of file and includes a manifest that contains import metadata such as permission requirements.<br />
<strong>b) Evidence :</strong> Refers to information about the origin of the code, such as a URL or a Zone. Such information is either in the assembly or provided by the host in which the assembly is executed.<br />
<strong>c) Policies :</strong> define a set of rules that determine the permissions to be granted to assemblies.<br />
<strong>d) Permissions :</strong> Objects that grant code access to resource and authority to perform tasks.<br />
<strong>e) Principals :</strong> object that represent both the identity and roles of a user. Evry thread or process is associated with a principal. By using role-based security. Permission can be granted and denied according to the identity or the roles of the principal.</p>
<p><span style="text-decoration:underline;"><strong>Stack walking</strong></span><br />
A powerful feature of the code access security architecture is that it can evaluate permission demand by walking up the call stack. For example, consider what happen when a method in one assembly call another assembly and in this second assembly, a request is made for a particular permission.<br />
its is possible that the evidence presented by the second assembly is enough to obtain the permission. however, it is also possible that the evidence in the first assembly does not grant permission on request. by default the runtime walks up the call stack to guard against such possibilities.</p>
<p>Reference :-<br />
<a title="Code Access Security" href="http://msdn.microsoft.com/en-us/library/930b76w0(v=vs.90).aspx">MSDN-1-Code Access Security</a><br />
<a href="http://msdn.microsoft.com/en-us/library/c5tk9z76(v=VS.85).aspx">MSDM-2-Code Access Security</a><br />
<a href="http://en.wikipedia.org/wiki/Code_Access_Security">Wikipedia-[Code Access Security]</a></p>
<br />Filed under: <a href='http://raviranjankr.wordpress.com/category/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/category/codeproject/'>CodeProject</a> Tagged: <a href='http://raviranjankr.wordpress.com/tag/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/tag/codeproject/'>CodeProject</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raviranjankr.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raviranjankr.wordpress.com/178/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=178&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raviranjankr.wordpress.com/2011/11/05/basic-introduction-of-code-access-security-for-beginners/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c33505bacfac61a7a64b55a03706ca80?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raviranjankr</media:title>
		</media:content>
	</item>
		<item>
		<title>Prevent Focus at Particular Cell of DataGridView</title>
		<link>http://raviranjankr.wordpress.com/2011/10/13/prevent-focus-at-particular-cell-of-datagridview/</link>
		<comments>http://raviranjankr.wordpress.com/2011/10/13/prevent-focus-at-particular-cell-of-datagridview/#comments</comments>
		<pubDate>Thu, 13 Oct 2011 20:28:04 +0000</pubDate>
		<dc:creator>Ravi Ranjan Kumar</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>

		<guid isPermaLink="false">http://raviranjankr.wordpress.com/?p=141</guid>
		<description><![CDATA[Introduction Some time we need to create an application in which Datagridview doesn&#8217;t allow to give functionality to user to put focus on a particular cell. its a very needed requirement which we face time to time. suppose a user &#8230; <a href="http://raviranjankr.wordpress.com/2011/10/13/prevent-focus-at-particular-cell-of-datagridview/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=141&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>Introduction </strong></p>
<p>Some time we need to create an application in which Datagridview doesn&#8217;t allow to give functionality to user to put focus on a particular cell. its a very needed requirement which we face time to time. suppose a user have a value and he doesn&#8217;t want to make it editable, and want to prevent it from any focus so in this case this article might help you</p>
<p><span style="text-decoration:underline;">You need to Know</span></p>
<p>like every control DataGridView have some by default value like DataGridView navigation model does not have any ability to restrict focus to a specific cell.</p>
<p>To implement functionality to give accesss user to prevent cell from focus of Users. we need to implement our own navigation logic by overriding the appropriate keyboard, navigation and mouse methods such as DataGridView.OnKeyDown, DataGridView.ProcessDataGridViewKey,DataGridView.SetCurrentCellAddressCore, DataGridView.OnMouseDown</p>
<p>Lets an example if you want to prevent user to give focus on second column of DataGridView. then in this case you need to drive DataGridView Class and Override the SetCurrentCellAddressCore and SetSelectedCellCore to accomplish the logic you want to perform.</p>
<p><span style="text-decoration:underline;">How it Will Look</span></p>
<p>you can see user is able to focus on almost every column except 1st(NationalIDNumber) Column of DataGridView control.</p>
<div id="attachment_142" class="wp-caption aligncenter" style="width: 510px"><a href="http://raviranjankr.files.wordpress.com/2011/09/dgvmanimation.gif"><img class="size-medium wp-image-142   " title="DataGridViewMain" src="http://raviranjankr.files.wordpress.com/2011/08/datagridviewmain.gif?w=500&#038;h=400" alt="" width="500" height="400" /></a><p class="wp-caption-text">Animated Image - Preventing Cell of DataGridView</p></div>
<p><span style="text-decoration:underline;"><strong>How to Do</strong></span></p>
<p>Take a look at given images and code to learn how to perform it.<br />
Its better to create an different class and for it you need to perform following steps as shown in Given figure.<br />
right Click on Project name under solution Explorer and then Select Add and then click on Class as shown below</p>
<p style="text-align:center;"><a href="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg1.png"><img class="aligncenter size-medium wp-image-148" src="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg1.png?w=500&#038;h=400" alt="" width="500" height="400" /></a></p>
<p>It will AddNewItem Form where you can select class and then need to name it(class)</p>
<p style="text-align:center;"><a href="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg2.png"><img class="aligncenter size-medium wp-image-149" title="Add New Item" src="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg2.png?w=500&#038;h=400" alt="" width="500" height="400" /></a></p>
<p>Now Select Class from Visual C# Items and enter the Name (Example <strong>MyDataGridView.cs</strong>)  in textbox and then press Add button.<br />
when you press Add button it will create a new class in your Program under Solution Explorer so after creating class we need to inherit DataGridView class with current class (MyDataGridView).<br />
take a look how</p>
<p style="text-align:center;"><a href="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg3.png"><img class="aligncenter size-medium wp-image-150" title="Inherit Class with DataGridView" src="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg3.png?w=500&#038;h=400" alt="" width="500" height="400" /></a></p>
<p>and DataGridView class is accessible only when you&#8217;ve declared<br />
Windows.Form namespace like.<br />
<strong>Note :-</strong> To access DataGridView class you need to include <strong>System.Window.Form</strong> namespace in your program.</p>
<pre class="brush: csharp; title: ; notranslate">

Using System.Windows.Forms

</pre>
<p>and then entire code of DataGridview class will be</p>
<pre class="brush: csharp; title: ; notranslate">
public class myDataGridView : DataGridView
   {
      private int columnToSkip = -1;
       public int ColumnToSkip
       {
           get { return columnToSkip; }
           set { columnToSkip = value; }
       }

       protected override bool SetCurrentCellAddressCore(int columnIndex, int rowIndex,
       bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick)
       {

           if (columnIndex == this.columnToSkip &amp;&amp; this.columnToSkip != -1)
           {
              if (this.columnToSkip == this.ColumnCount - 1)
               {
                  return base.SetCurrentCellAddressCore(0, rowIndex + 1,
                      setAnchorCellAddress, validateCurrentCell, throughMouseClick);
               }
               else
               {
                   if (this.ColumnCount != 0)
                   {
                       return base.SetCurrentCellAddressCore(columnIndex + 1, rowIndex,
                          setAnchorCellAddress, validateCurrentCell, throughMouseClick);
                   }
               }
           }
          return base.SetCurrentCellAddressCore(columnIndex, rowIndex,
               setAnchorCellAddress, validateCurrentCell, throughMouseClick);
       }

       protected override void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)
       {
           if (columnIndex == this.columnToSkip)
           {
              if (this.columnToSkip == this.ColumnCount - 1)
               {
                  base.SetSelectedCellCore(0, rowIndex + 1, selected);
               }
              else
               {
                   if (this.ColumnCount != 0)
                   {
                    base.SetSelectedCellCore(columnIndex + 1, rowIndex, selected);
                   }
               }
           }
          else
           {
              base.SetSelectedCellCore(columnIndex, rowIndex, selected);
           }
      }
   }
</pre>
<p style="text-align:center;">when collapse each and every function of class. program will look like as given below <a href="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg4.png"><img class="aligncenter size-medium wp-image-151" title="Function After Collapse" src="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg4.png?w=500&#038;h=400" alt="" width="500" height="400" /></a></p>
<p>Now Compile application once time to came existence of myDataGridView control in toolbox. after one successful execution it will showing in Toolbox as given below and then we can drag it from toolbox to Form and can use it with some additional functionality</p>
<p style="text-align:center;"><a href="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg5.png"><img class="aligncenter size-medium wp-image-151" title="Drag Control over Form" src="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg5.png?w=500&#038;h=400" alt="" width="500" height="400" /></a></p>
<p>Now you need to write query to &#8221; bind DataGridView with your database &#8221; as give below<br />
here I am using <strong>AdventureWorks</strong> database to bind DataGridView<br />
you can see at last two circled line in which 1st line is responsible to bind DataGridview to datasource and very last line is responsible for skipping value of 1st column of DataGridView<br />
Take a look</p>
<p style="text-align:center;"><a href="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg7.png"><img class="aligncenter size-medium wp-image-151" title="code showing how to use user Control (MyDataGridView1) property  " src="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg7.png?w=500&#038;h=400" alt="" width="500" height="400" /></a></p>
<p>Now Press <strong>F5</strong> or Compile it to see Effect<br />
you will notice every columns is accessible Except 1st Column which is bounded with red Circled.</p>
<div id="attachment_151" class="wp-caption aligncenter" style="width: 510px"><a href="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg6.png"><img class="size-medium wp-image-151" src="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg6.png?w=500&#038;h=400" alt="" width="500" height="400" /></a><p class="wp-caption-text">A snap of of compiled program</p></div>
<p>So Finally we learned how we can prevent to focusing on particular cell of DataGridView <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To get more practical view you can download source code also.<br />
<strong><a title="Click Me.. to Download Source code. " href="http://dl.dropbox.com/u/27553051/DotNet%20C%23/DataGridViewPractical.zip">Click Me!!</a></strong>   to download Source code.</p>
<br />Filed under: <a href='http://raviranjankr.wordpress.com/category/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/category/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/category/codeproject/'>CodeProject</a> Tagged: <a href='http://raviranjankr.wordpress.com/tag/net/'>.Net</a>, <a href='http://raviranjankr.wordpress.com/tag/c/'>C#</a>, <a href='http://raviranjankr.wordpress.com/tag/codeproject/'>CodeProject</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raviranjankr.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raviranjankr.wordpress.com/141/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raviranjankr.wordpress.com&#038;blog=22873522&#038;post=141&#038;subd=raviranjankr&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raviranjankr.wordpress.com/2011/10/13/prevent-focus-at-particular-cell-of-datagridview/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c33505bacfac61a7a64b55a03706ca80?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raviranjankr</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2011/08/datagridviewmain.gif?w=300" medium="image">
			<media:title type="html">DataGridViewMain</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg1.png?w=300" medium="image" />

		<media:content url="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg2.png?w=300" medium="image">
			<media:title type="html">Add New Item</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg3.png?w=300" medium="image">
			<media:title type="html">Inherit Class with DataGridView</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg4.png?w=300" medium="image">
			<media:title type="html">Function After Collapse</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg5.png?w=300" medium="image">
			<media:title type="html">Drag Control over Form</media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg7.png?w=300" medium="image">
			<media:title type="html">code showing how to use user Control (MyDataGridView1) property  </media:title>
		</media:content>

		<media:content url="http://raviranjankr.files.wordpress.com/2011/08/dgpfimg6.png?w=300" medium="image" />
	</item>
	</channel>
</rss>
