**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 .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.

**Encryption :**

Encryption is a process of converting useful **data(Plain text) into ciphertext(ciphertext is called Encrypted Text) format** 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.

** Decryption :**

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.

Have a look at below fig :

Animated Pic – Encryption and Decryption

**Symmetric Encryption :**

It is also called Single Key, private Key or **Secret Key Cryptography(SKC)**.while using Symmetric Encryption only a single key is used for both encryption and decryption process.

Animated Pic:- Symmetric Encryption Decryption Concept

Symmetric encryption algorithm are always considered as less complex than asymmetric algorithm because only single key is being used in Symmetric encryption.

**Symmetric Algorithm classes :**

System.Security.Cryptography

namespace provides encryption classes that provide most popular symmetric algorithms like

- Data Encryption Standard (DES)
- Triple Data Encryption Standard
- (Rivest Cipher) RC2
- Rijndael
- Advanced Encryption Standard (AES) [Supported in NET Framework 3.5 and 4.0 only ]

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 DES, TripleDES, RC2 and Rijndael classes extend the SymmetricAlgorithm classes and also provide symmetric encryption in which each and every of these classes is further extended by its correspoding CryptoServiceProvider(CSP) class. CSP class are kind of concrete class that gives service of Cryptography by wrapping unmanaged object that are external to Common Language Runtime(CLR) Lets see One by One Implementation DES Class : The Data Encryption Standard(DES) class is an abstract(MustInherit) class that extends the SymmetricAlgorithm class and also provides support for its algorithm. The DESCryptoServiceProvider 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.

**Implementation of the DES class**

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.

The following code sample guide you how to use and implement the DESCryptoServiceProvider class.

To perform Encryption and Decryption. you must add

using System.Security.Cryptography;

Now take a look at encryption function

public static byte[] Encrypt(string strText, SymmetricAlgorithm key)
{
MemoryStream ms = new MemoryStream();
CryptoStream crypstream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(crypstream);
sw.WriteLine(strText);
sw.Close();
crypstream.Close();
byte[] buffer = ms.ToArray();
ms.Close();
return buffer;
}

In above code, CreateEncryptor() function is used to encryptor object with the current Key property and initialization vector.

Now in same way we need to create function for Decrypt the PlainText(Encrypted Text)

have a look at given function which is responsible to decrypt encrypted text.

public static string Decrypt(byte[] encryptText, SymmetricAlgorithm key)
{
MemoryStream ms = new MemoryStream(encryptText);
CryptoStream crypstream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(crypstream);
string val = sr.ReadLine();
sr.Close();
crypstream.Close();
ms.Close();
return val;
}

In above code, decryption is handled in same way by using CreateDecryptor() function instead of CreateEncryptor()

Now we have created function, so we can use both function to appropriate manner to accomplishment of Encryption Decryption task.

**How to use Encrypt Function**

DESCryptoServiceProvider key = new DESCryptoServiceProvider();
byte[] buffer = Encrypt("ABC", key);

**How to use Decrypt Function**

DESCryptoServiceProvider key = new DESCryptoServiceProvider();
byte[] buffer = Encrypt("ABC", key);
string strText = Decrypt(buffer, key);

**Download Source Code :**

Want to Download Source Code : Click Me!

**References :**

**Coming Next **

In the next article, hopefully I will come back with other symmetric algorithm implementation like AES, RC2, Rijndael and TripleDES for beginners.

Filed under:

.Net,

C#,

CodeProject Tagged:

.Net,

C#,

CodeProject