Link List (How to develop with C#)

AN OBJECT-ORIENTED LINKED LIST DESIGN
Our design of a linked list will involve at least two classes.We’ll create a Node class and instantiate a Node object each time we add a node to the list. The nodes in the list are connected via references to other nodes. These references are set using methods created in a separate LinkedList class.Let’s start by looking at the design of the Node class.
The Node Class
A node is made up of two data members: Element, which stores the node’s data; and Link, which stores a reference to the next node in the list.We’ll use Object for the data type of Element, just so we don’t have to worry about what kind of data we store in the list. The data type for Link is Node, which seems strange but actually makes perfect sense. Since we want the link to point to the next node, and we use a reference to make the link, we have to assign a Node type to the link member.
To finish up the definition of the Node class, we need at least two constructor methods. We definitely want a default constructor that creates an empty Node, with both the Element and Link members set to null. We also need a parametrized constructor that assigns data to the Element member and sets the Link member to null.
Here’s the code for the Node class:

public class Node
{
public Object Element;
public Node Link;
public Node()
{
Element = null;
Link = null;
}
public Node(Object theElement)
{
Element = theElement;
Link = null;
}
}

The LinkedList Class
The LinkedList class is used to create the linkage for the nodes of our linked list. The class includes several methods for adding nodes to the list, removing nodes from the list, traversing the list, and finding a node in the list. We also need a constructor method that instantiates a list. The only data member in the class is the header node.

public class LinkedList
{
protected Node header;
public LinkedList()
{
header = new Node("header");
}
. . .
}

The header node starts out with its Link field set to null. When we add the first node to the list, the header node’s Link field is assigned a reference to the new node, and the new node’s Link field is assigned the null value.

To insert a new node after an existing node

To do this, we create a Private method, Find, that searches through the Element field of each node until a match is found.

private Node Find(Object item)

{
Node current = new Node();
current = header;
while(current.header != item)
current = current.Link;
return current;
}

Once we’ve found the “after” node, the next step is to set the new node’s Link field to the Link field of the “after” node, and then set the “after” node’s Link field to a reference to the new node. Here’s how it’s done:

public void Insert(Object newItem, Object after)

{
Node current = new Node();
Node newNode = new Node(newItem);
current = Find(after);

newNode.Link = current.Link;
current.Link = newNode;
}


Remove a node from a linked list

we simply have to change the link of the node that points to the removed node to point to the node after the removed node.

private Node FindPrevious(Object n)

{
Node current = header;
while(!(current.Link == null) && (current.Link.
Element != n))
current = current.Link;
return current;
}

Now we’re ready to see how the code for the Remove method looks:

public void Remove(Object n)

{
Node p = FindPrevious(n);
if (!(p.Link == null))
p.Link = p.Link.Link;
}

PrintList

public void PrintList()

{
Node current = new Node();
current = header;
while (!(current.Link == null))

{
Console.WriteLine(current.Link.Element);
current = current.Link;
}
}

Link List (Howz it works)

A linked list is a collection of class objects called nodes. Each node is linked to its successor node in the list using a reference to the successor node. A node is made up of a field for storing data and the field for the node reference. The reference to another node is called a link. An example linked list is:

A major difference between an array and a linked list is that whereas the elements in an array are referenced by position (the index), the elements of a linked list are referenced by their relationship to the other elements of the array. From above example we can say that “Bread” follows “Milk”, not that “Bread” is in the second position. Moving through a linked list involves following the links from the beginning node to the ending node.
Another thing to notice that we mark the end of a linked list by pointing to the value null. Since we are working with class objects in memory, we use the null object to denote the end of the list.
Marking the beginning of a list can be a problem in some cases. It is common in many linked list implementations to include a special node, called the “header”, to denote the beginning of a linked list. The linked list in underlying figure is redesigned with a header node:
Insertion and Removal of an Item from Link List
Insertion becomes a very efficient task when using a linked list. All that is involved is changing the link of the node previous to the inserted node to point to the inserted node, and setting the link of the new node to point to the node the previous node pointed to before the insertion. In Figure (below) the item “Cookies” is added to the linked list after “Eggs”.
Removing an item from a linked list is just as easy. We simply redirect the link of the node before the deleted node to point to the node the deleted node points to and set the deleted node’s link to null. The diagram (below) we remove “Bacon” from the linked list.


Pointers and unsafe code in c#

Introduction

We will see that C# allows suspending the verification of code by the CLR to allow developers to directly access memory using pointers. Hence with C#, you can complete, in a standard way, certain optimizations which were only possible within unmanaged development environments such as C++. These optimizations concern, for example, the processing of large amounts of data in memory such as bitmaps.

Pointers and unsafe code

C++ does not know the notion of code management. This is one of the advantages of C++ as it allows the use of pointers and thus allows developers to write optimized code which is closer to the target machine.

This is also a disadvantage of C++ since the use of pointers is cumbersome and potentially dangerous, significantly increasing the development effort and maintenance required.

Before the .NET platform, 100% of the code executed on the Windows operating system was unmanaged. This means the executable contains the code directly in machine instructions which are compatible with the type of processor (i.e. machine language code). The introduction of the managed execution mode with the .NET platform is revolutionary. The main sources of hard to track bugs are detected and resolved by the CLR. Amongst these:

  • Array access overflows (Now dynamically managed by the CLR).
  • Memory leaks (Now mostly managed by the garbage collector).
  • The use of an invalid pointer. This problem is solved in a radical way as the manipulation of pointers if forbidden in managed mode.

The CLR knows how to manipulate three kinds of pointers:

  • Managed pointers. These pointers can point to data contained in the object heap managed by the garbage collector. These pointers are not used explicitly by the C# code. They are thus used implicitly by the C# compiler when it compiles methods with out and ref arguments.

  • Unmanaged function pointers. The pointers are conceptually close to the notion of delegate. We will discuss them at the end of this article.

  • Unmanaged pointers. These pointers can point to any data contained in the user addressing space of the process. The C# language allows to use this type of pointers in zones of code considered unsafe. The IL code emitted by the C# compiler corresponding to the zones of code which use these unmanaged pointers make use of specialized IL instructions. Their effect on the memory of the process cannot be verified by the JIT compiler of the CLR. Consequently, a malicious user can take advantage of unsafe code regions to accomplish malicious actions. To counter this weakness, the CLR will only allow the execution of this code at run-time if the code has the SkipVerification CAS meta-permission.

Since it allows to directly manipulating the memory of a process through the use of an unmanaged pointer, unsafe code is particularly useful to optimize certain processes on large amounts of data stored in structures.

Compilation options to allow unsafe code

Unsafe code must be used on purpose and you must also provide the /unsafe option to the csc.exe compiler to tell it that you are aware that the code you wish to compile contains zones which will be seen as unverifiable by the JIT compiler. Visual Studio offers the Build Allow unsafe code project property to indicate that you wish to use this compiler option.

Declaring unsafe code in C#

In C#, the unsafe keyword lets the compiler know when you will use unsafe code. It can be used in three situations:

  • Before the declaration of a class or structure. In this case, all the methods of the type can use pointers.

  • Before the declaration of a method. In this case, the pointers can be used within the body of this method and in its signature.

  • Within the body of a method (static or not). In this case, pointers are only allowed within the marked block of code. For example:

    unsafe
    {
    ...
    }

Let us mention that if a method accepts at least one pointer as an argument or as a return value, the method (or its class) must be marked as unsafe, but also all regions of code calling this method must also be marked as unsafe.


Using pointers in C#

Each object, whether it is a value or reference type instance, has a memory address at which it is physically located in the process. This address is not necessarily constant during the lifetime of the object as the garbage collector can physically move objects store in the heap.

.NET types that support pointers

For certain types, there is a dual type, the unmanaged pointer type which corresponds to the managed type. A pointer variable is in fact the address of an instance of the concerned type. The set of types which authorizes the use of pointers limits itself to all value types, with the exception of structures with at least one reference type field. Consequently, only instances of the following types can be used through pointers: primitive types; enumerations; structures with no reference type fields; pointers.

Declaring pointers

A pointer might point to nothing. In this case, it is extremely important that its value should be set to null (0). In fact, the majority of bugs due to pointers come from pointers which are not null but which point to invalid data. The declaration of a pointer on the FooType is done as follows:

FooType * pointeur;

For example:

long * pAnInteger = 0;

Note that the declaration...

int * p1,p2;

... makes it so that p1 is a pointer on an integer and p2 is a pointer.


Now let's see the first program

Program 1


using System;

class MyClass {
public static void Main() {
int iData = 10;
int* pData = &iData;
Console.WriteLine("Data is " + iData);
Console.WriteLine("Address is " + (int)pData );
}

Here I use a pointer in this program. Now compile this program. The compiler gives the error

Now let's change the program a little bit and add unsafe modifier with the function.

Program 2

using System;

class MyClass {
public unsafe static void Main() {
int iData = 10;
int* pData = &iData;
Console.WriteLine("Data is " + iData);
Console.WriteLine("Address is " + (int)pData );
}
Data is 10
Address is 1244316

It is not necessary that we define the unsafe modifier with the function. We can define a block of unsafe code. Let's change a program little bit more.

Program 3


using System;

class MyClass {
public static void Main() {
unsafe {
int iData = 10;
int* pData = &iData;
Console.WriteLine("Data is " + iData);
Console.WriteLine("Address is " + (int)pData );
}
}
}

In this program a block is defined with unsafe modifier. So we can use pointers in that code. The output of this program is the same as previous one.

Now let's change the program a little bit to get a value from the pointer.

Program 4

using System;

class MyClass {
public static void Main() {
unsafe {
int iData = 10;
int* pData = &iData;
Console.WriteLine("Data is " + iData);
Console.WriteLine("Data is " + pData->ToString() );
Console.WriteLine("Address is " + (int)pData );
}
}
}
Program 5
using System;

class MyClass {
public static void Main() {
testFun();
}

public static unsafe void testFun() {
int iData = 10;
int* pData = &iData;
Console.WriteLine("Data is " + iData);
Console.WriteLine("Address is " + (int)pData );
}

In this program a function with unsafe modifier is called from a normal function. This program shows that a managed code can call unmanaged functions. The output of the program is the same as previous program.

Now change the program little bit and make an unsafe function in another class.

Program 6


using System;

class MyClass {
public static void Main() {
TestClass Obj = new TestClass();
Obj.testFun();
}
}

class TestClass {
public unsafe void testFun() {
int iData = 10;
int* pData = &iData;
Console.WriteLine("Data is " + iData);
Console.WriteLine("Address is " + (int)pData );
}
}

The output of the program is same as previous one.

Now try to pass pointer as a parameter. Let’s see this program.

Program 7


using System;

class MyClass {
public static void Main() {
TestClass Obj = new TestClass();
Obj.testFun();
}
}

class TestClass {
public unsafe void testFun() {
int x = 10;
int y = 20;
Console.WriteLine("Before swap x = " + x + " y= " + y);
swap(&x, &y);
Console.WriteLine("After swap x = " + x + " y= " + y);
}

public unsafe void swap(int* p_x, int *p_y) {
int temp = *p_x;
*p_x = *p_y;
*p_y = temp;
}
}

In this program the unsafe function testFun() calls the classic swap() function to interchange the value of two variables passing by reference. Now change the program a little bit.

Program 8


using System;

class MyClass {
public static void Main() {
TestClass Obj = new TestClass();
unsafe {
int x = 10;
int y = 20;
Console.WriteLine("Before swap x = " + x + " y= " + y);
Obj.swap(&x, &y);
Console.WriteLine("After swap x = " + x + " y= " + y);
}
}
}

class TestClass {
public unsafe void swap(int* p_x, int* p_y) {
int temp = *p_x;
*p_x = *p_y;
*p_y = temp;
}
}

This program does the same job as previous one. But in this program we write only one unsafe function and call this function from the unsafe block in Main.

Now let's see another program which show the usage of array in C#

Program 9


using System;

class MyClass {
public static void Main() {
TestClass Obj = new TestClass();
Obj.fun();
}
}

class TestClass {
public unsafe void fun() {
int [] iArray = new int[10];

// store value in array
for (int iIndex = 0; iIndex < 10; iIndex++) {
iArray[iIndex] = iIndex * iIndex;
}

// get value from array
for (int iIndex = 0; iIndex < 10; iIndex++) {
Console.WriteLine(iArray[iIndex]);
}
}
}

This program display the square of numbers from zero to 9.

Let's change the program a little bit and pass the array as a parameter to a function.

Program 10


using System;

class MyClass {
public static void Main() {
TestClass Obj = new TestClass();
Obj.fun();
}
}

class TestClass {
public unsafe void fun() {
int [] iArray = new int[10];

// store value in array
for (int iIndex = 0; iIndex < 10; iIndex++) {
iArray[iIndex] = iIndex * iIndex;
}

testFun(iArray);
}

public unsafe void testFun(int [] p_iArray) {

// get value from array
for (int iIndex = 0; iIndex < 10; iIndex++) {
Console.WriteLine(p_iArray[iIndex]);
}
}
}

The output of the program is same as previous one.

Now let's change the program a little bit and try to get the value of the array from a pointer rather than an index.

Program 11


using System;

class MyClass {
public static void Main() {
TestClass Obj = new TestClass();
Obj.fun();
}
}

class TestClass {
public unsafe void fun() {
int [] iArray = new int[10];

// store value in array
for (int iIndex = 0; iIndex < 10; iIndex++) {
iArray[iIndex] = iIndex * iIndex;
}

// get value from array
for (int iIndex = 0; iIndex < 10; iIndex++) {
Console.WriteLine(*(iArray + iIndex) );
}
}
}

Here in this program we try to access the value of the array from *(iArray + iIndex) rather than iArray[iIndex]. But the program gives the following error.

Microsoft (R) Visual C# Compiler Version 7.00.9030 [CLR version 1.00.2204.21]
Copyright (C) Microsoft Corp 2000. All rights reserved.

um11.cs(21,24): error CS0019: Operator '+' cannot be applied to operands of type 'int[]' and 'int'

In C# int* and in[] are not treated the same. To understand it more let's see one more program.

using System;

class MyClass {
public static void Main() {
TestClass Obj = new TestClass();
Obj.fun();
}
}

class TestClass {
public unsafe void fun() {
int [] iArray = new int[10];
iArray++;

int* iPointer = (int*)0;
iPointer++;

}
}

There are two different types of variable in this program. First, the variable iArray is declared an array and the second variable iPointer is a pointer variable. Now I am going to increment both. We can increment the pointer variable because it is not fixed in memory but we can't increment the iArray, because the starting address of the array is stored in iArray and if we are allowed to increment this then we will lose starting address of array.

The output of the program is an error.

Microsoft (R) Visual C# Compiler Version 7.00.9030 [CLR version 1.00.2204.21]
Copyright (C) Microsoft Corp 2000. All rights reserved.

um12.cs(13,3): error CS0187: No such operator '++' defined for type 'int[]'

To access the element of the array via a pointer we have to fix the pointer so it can't be incremented. C# uses the fixed reserve word to do this.

Program 13


using System;

class MyClass {
public static void Main() {
TestClass Obj = new TestClass();
Obj.fun();
}
}

class TestClass {
public unsafe void fun() {
int [] iArray = new int[10];

// store value in array
for (int iIndex = 0; iIndex < 10; iIndex++) {
iArray[iIndex] = iIndex * iIndex;
}

// get value from array
fixed(int* pInt = iArray)
for (int iIndex = 0; iIndex < 10; iIndex++) {
Console.WriteLine(*(pInt + iIndex) );
}
}
}

We can use the same technique to pass the array to a function which receives the pointer as a parameter.

Program 14


using System;

class MyClass {
public static void Main() {
TestClass Obj = new TestClass();
Obj.fun();
}
}

class TestClass {
public unsafe void fun() {
int [] iArray = new int[10];

// store value in array
for (int iIndex = 0; iIndex < 10; iIndex++) {
iArray[iIndex] = iIndex * iIndex;
}

// get value from array
fixed(int* pInt = iArray)
testFun(pInt);
}

public unsafe void testFun(int* p_pInt) {

for (int iIndex = 0; iIndex < 10; iIndex++) {
Console.WriteLine(*(p_pInt + iIndex) );
}
}
}

The output of the program is the same as the previous one. If we try to access beyond the array limit then it will print garbage.

Program 15
using System;

class MyClass {
public static void Main() {
TestClass Obj = new TestClass();
Obj.fun();
}
}

class TestClass {
public unsafe void fun() {
int [] iArray = new int[10];

// store value in array
for (int iIndex = 0; iIndex < 10; iIndex++) {
iArray[iIndex] = iIndex * iIndex;
}

// get value from array
fixed(int* pInt = iArray)
testFun(pInt);
}

public unsafe void testFun(int* p_pInt) {

for (int iIndex = 0; iIndex &lt 20; iIndex++) {
Console.WriteLine(*(p_pInt + iIndex) );
}
}
}

Here we try to read 20 elements from array but there are only 10 elements in the array so it will print garbage after printing the elements of array.

Program 16


using System;

struct Point {
public int iX;
public int iY;
}

class MyClass {
public unsafe static void Main() {

// reference of point
Point refPoint = new Point();
refPoint.iX = 10;
refPoint.iY = 20;

// Pointer of point
Point* pPoint = &refPoint;

Console.WriteLine("X = " + pPoint->iX);
Console.WriteLine("Y = " + pPoint->iY);

Console.WriteLine("X = " + (*pPoint).iX);
Console.WriteLine("Y = " + (*pPoint).iY);

}
}

Here pPoint is the pointer of Point class instance. We can access the element of it by using the -> Operator.

Change in Beta 2

When you want to compile program using command line switch you type the program name after the compiler name; for example if your program name is prog1.cs then you will compile this:

scs prog1.cs

This works fine for unsafe code while you are programming in beta 1. In beta 2 Microsft added one more switch to command line compiler of C# for writing unsafe code. Now if you want to write unsafe code then you have to specify the /unsafe command line switch with command line compiler otherwise the compiler gives an error. In beta 2 if you want to write unsafe code in your program then you compile your programas follows:

csc /unsafe prog1.cs















Cryptography in .NET

This article gives a brief overview of Cryptography and the Cryptography support provided by the .NET Framework. I begin by introducing Cryptography and then proceed to examine the various types of it. In particular, I review and analyze the various cryptography algorithms and objects supported by .NET. I conclude after proposing and briefly discussing the algorithmic technique that would work best for you.
Cryptography
I remember as kids, we would often play a game called 'jumble the word', where in we would replace an alphabet of a word with another. This way, A would be replaced with C; B with D and so on. Only someone who could understand this algorithm( in this case shift by 2), could decipher these messages and tell the word. Well in fact, this is 'Cryptography'. Surprisingly, we often use cryptography without consciously knowing it. For example, you may've tried to pass on a secret message to your friend using signals that only the two of you understood, or scribbled some text whose meaning was known only to you. We have all done that. Well....so we begin.
Cryptography is the science of scrambling meaningful characters into non-meaningful characters so that people who do not have access to the data cannot read it. The science of cryptography has been around for many years, even long before computers. Cryptography, over the ages, has been an art practiced by many who have devised different techniques to meet some of the information security requirements. The last twenty years have been a period of transition as the discipline moved from an art to a science. With the advent of computers, however, the science was able to produce almost unbreakable codes.
Well, Cryptography has been considered as one of the most complex aspect used by a developer. Using cryptographic algorithms and techniques is not considered a child's play, as it requires a high level of mathematical knowledge. Fortunately, with Microsoft .NET, newly created classes wrap up these sophisticated algorithms into fairly easy-to-use properties and methods. This article gives you an overview of the cryptography support that is provided by the .NET Framework.
However lets first look into a few jargons to make you familiar with cryptography :
1. Data that can be read and understood without any special measures is called 'plaintext' or 'cleartext'
2.The method of disguising plaintext in such a way as to hide its meaning is called 'Encryption'.
3.Encrypting plaintext results in unreadable chunks of data called 'Ciphertext'. You use encryption to make sure that information is hidden from anyone for whom it is not intended, even those who can see the encrypted data.
4.The process of reverting ciphertext to its original plaintext is called 'Decryption'.
5.And finally 'key' is a string of bits used for encrypting and decrypting information to be transmitted. It is a randomly generated set of numbers/ characters that is used to encrypt/decrypt information.
Types of Cryptography
After getting familiar with the terminology, let's delve into the types of Cryptography. There are two types of cryptography: private-key encryption and public-key encryption
Private key Encryption
Private Key encryption, also referred to as conventional or symmetric or single-key encryption was the only available option prior to the advent of Public Key encryption in 1976. This form of encryption was used by emperors like Julius Caesar and other military organizations to convey secret messages. This key requires all communicating parties, to share a common key. With private-key encryption, you encrypt a secret message using a key that only you know. To decrypt the message, you need to use the same key. Private-key cryptography is effective only if the key can be kept secret. Despite the potential weakness of private-key encryption, it is very easy to implement and computationally doesn't consume excessive resources.
Lets see an example - Imagine Julius is trying to send a secret message to his army chief, using a private key. In order for his chief to decrypt the secret message, he must know the private key. So Julius needs to send the key to him. But if the secrecy of his key is known to his opponents somehow , the message remains no longer secure. Moreover, if the Chief tells his subordinate about the private key, he can then also decrypt the message.
Public-key encryption
Public key encryption algorithms are based on the premise that each sender and recipient has a private key, known only to him/her and a public key, which can be known by anyone. Each encryption/decryption process requires at least one public key and one private key. Each is related to the other mathematically, such that messages encrypted with the public key can only be decrypted with the corresponding private key.
Lets see an example - Before Julius sends a message to his chief, he needs to generate the key pair containing the private key and the public key. The chief then freely distributes the public key to his subordinates but keeps the private key to himself. When Julius wants to send a message to his chief, he uses his public key to encrypt the message and then send it to him. Upon receiving the encrypted message, the Chief proceeds to decrypt it with his private key. In this case, he's the only one who can decrypt the message, since the key pair works in such a way that only messages encrypted with the public key can be decrypted with the private key. Also, there's no need to exchange secret keys, thus eliminating the risk of compromising the secrecy of the key.The reverse can happen as well. Suppose the Chief sends a message encrypted with his private key to Julius. To decrypt the message, Julius need his public key. But what's the point of that? The public key isn't a secret-everyone knows it. However, using this method guarantees that the message hasn't been tampered with and is indeed from his chief and not his opponents. If the message had been modified, Julius wouldn't be able to decrypt it.
I wish he was here to read all this!!!
.NET and Cryptography
.NET provides a set of cryptographic objects, supporting well-known algorithms and common uses including hashing, encryption, and generating digital signatures. These objects are designed in a manner that facilitates the incorporation of these basic capabilities into more complex operations, such as signing and encrypting a document. Cryptographic objects are used by .NET to support internal services, but are also available to developers who need cryptographic support. The .NET Framework provides implementations of many such standard cryptographic algorithms and objects. Similar to the ready availability of simple authentication features within the .NET Framework, cryptographic primitives are also easily accessible to developers via stream-based managed code libraries for encryption, digital signatures, hashing, and random number generation. The System.Security.Cryptography namespace in the .NET Framework provides these cryptographic services. The Algorithm support includes:
RSA and DSA public key (asymmetric) encryption - Asymmetric algorithms operate on fixed buffers. They use a public-key algorithm for encryption/decryption. An example for asymmetric algorithms is the RSA algorithm which is so named after its three inventors Rivest, Shamir, and Adleman. It is a popular public-key algorithm - the de facto standard - for digital signatures and can be used for encryption as well. The DSA_CSP is an implementation of the digital signature algorithm (DSA). This is a public-key algorithm. It can be used to create and verify a digital signature.
DES, TripleDES, and RC2 private key (symmetric) encryption - Symmetric algorithms are used to modify variable length buffers and perform one operation for periodical data input. They use a single secret key to encrypt and decrypt data.The Data Encryption Standard (DES) is a world-wide standard for data encryption, which was published in the early 1970s. It is the most popular encryption algorithm. It is implemented by the DES_CSP class. This class represents a stream where you pour in data that is encrypted/decrypted using a single key. The Triple DES encryption algorithm operates on a block of data three times using one key. RC2 stands for Rivest Cipher or "Ron's Code", which is the name of its inventor. RC2 is a symmetric encryption algorithm and works with a variable key-size. it is a block cipher, like many other .NET cryptography algorithms, that operates on groups of bits in contrast to stream cipher algorithms.
MD5 and SHA1 hashing - MD5 - Message Digest 5-is a one-way hash algorithm. Given variable length data as input it always produces a 128-bit hash value. The Secure Hash Algorithm (SHA) also is a one-way hash algorithm that produces a 160-bit hash value, which is longer than the MD5 produced hash value.
(You must have observed the word CSP. Well CSP is a Cryptographic Service Provider. It is the entity that performs the cryptographic computations. The CSP classes are derived from the corresponding base classes and implement a solution for a specific algorithm. For example, the DESCryptoServiceProvider class is derived from the DES class and implements the digital encryption standard. You can use the provided classes or implement your own solution. )

Here is a general guideline to help you decide when to use which method
Symmetric, or secret key, algorithms are extremely fast and are well suited for encrypting large streams of data. These algorithms, both encrypt and decrypt data. While these are fairly secure, they do have the potential to be broken given enough time, as someone could do a search on every known key value combination. Since each of these algorithms uses a fixed key length or ASCII characters, it is feasible that a computer program could try every possible combination of keys and eventually stumble onto the right one. A common use of these types of algorithms is for storing and retrieving connection strings to databases.
Asymmetric, or public key, algorithms are not as fast as symmetric, but are much harder codes to break. These algorithms rely on two keys, one is Private and the other is Public. The public key is used to encrypt a message. The Private key is the only one that can decrypt the message. The public and private keys are mathematically linked and thus both are needed for this cryptographic exchange to occur successfully. Asymmetric algorithms are not well suited to large amounts of data due to performance. One common use of asymmetric algorithms is to encrypt and transfer to another party a symmetric key and initialization vector. The symmetric algorithm is then used for all messages being sent back and forth.
Hash values are used when you do not wish to ever recover the original value and you especially wish for no one else to discover the original value as well. Hashes will take any arbitrary string length and hash it to a fixed set of bytes. This operation is one-way, and thus is typically used for small amounts of data, like a password. If a user inputs a user password into a secure entry screen, the program can hash this value and store the hashed value into a database. Even if the database were compromised, no one would be able to read the password since it was hashed. When the user then logs into the system to gain entry, the password typed in is hashed using the same algorithm, and if the two hashed values match, then the system knows the input value was the same as the saved value from before.
Everyone Loves an Example
Everyone needs and loves a good example. After having read about the various algorithms available, lets see an example of encrypting and decrypting files using the System.Security.Cryptography namespace. I have used the Rijndael Managed encryption method. The Rijndael Managed class accesses the managed version of the Rijndael algorithm. This class cannot be inherited. The Rijndael class represents the base class from which all implementations of the Rijndael symmetric encryption algorithm must inherit.
The hierarchy is as follows
System.Object
System.Security.Cryptography.SymmetricAlgorithm
System.Security.Cryptography.Rijndael
System.Security.Cryptography.RijndaelManaged
// Encrypting and decrypting files using the Rijndael Managed encryption method.
using System;
using System.IO;
using System.Security.Cryptography;
class CryptoEx
{
public static void Main(string[] args)
{
if (args.Length!=1)
{
Console.WriteLine("FileName Not Entered. Specify a filename to encrypt.");
return;
}
string file = args[0];
string tempfile = Path.GetTempFileName(); // Open the file to read
FileStream fsIn = File.Open(file,FileMode.Open,FileAccess.Read);
FileStream fsOut = File.Open(tempfile, FileMode.Open,FileAccess.Write);
SymmetricAlgorithm symm = new RijndaelManaged();
//creating an instance
ICryptoTransform transform = symm.CreateEncryptor();
//and calling the CreateEncryptor method which
//creates a symmetric encryptor object.
CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write); BinaryReader br = new BinaryReader(fsIn);
cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length);
cstream.FlushFinalBlock();
cstream.Close();
fsIn.Close();
fsOut.Close();
Console.WriteLine("Created Encrypted File {0}", tempfile);
fsIn = File.Open(tempfile,FileMode.Open,FileAccess.Read);
transform = symm.CreateDecryptor();
cstream = new CryptoStream(fsIn,transform,CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cstream);
Console.WriteLine("Decrypted the File: " + sr.ReadToEnd());
fsIn.Close();
}
Summary :
We saw that the .NET Framework supports encryption by means of cryptographic streaming objects based on the primitives. It also supports digital signatures, message authentication codes (MACs)/keyed hash, pseudo-random number generators (PRNGs), and authentication mechanisms. New or pre-standard primitives as SHA-256 or XMLDSIG are already supported. The ready availability of such libraries is hopefully going to drive more widespread reliance on Cryptography to fortify the security of everyday applications. Based on our own experiences, we can confidently state that well-implemented cryptography dramatically increases the security of many aspects of a given application.