Click here to Skip to main content
12,694,842 members (32,739 online)
Click here to Skip to main content
Add your own
alternative version

Stats

23.5K views
943 downloads
13 bookmarked
Posted

Cryptography: Symmetric Encryption by Symmetric Algorithm Classes – Part 1

, 18 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This blog will enable you to understand the basic of Cryptography with Symmetric Encryption Algorithm Classes.

Cryptographic

Cryptography refer to a process or a mechanism which enables us to read and write sensitive or secure data. We can find Cryptography features in all versions of .NET Framework from 1.0 to 4.0. However, the latest version of the .NET framework providing new types and significant enhancement to existing types can support symmetric and asymmetric encryption and hashing 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. During decryption, we need to use the 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 is implemented both the Symmetric and asymmetric encryption algorithm.

Have a look at the below figure:

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 algorithms 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 abstract(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 one of these classes is further extended by its corresponding CryptoServiceProvider(CSP) class. CSP classes are kind of concrete classes that give service of Cryptography by wrapping unmanaged object that are external to Common Language Runtime(CLR). Let's 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 is supporing DES algorithm because of its huge 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 is generated by a complex mathematical algorithm, or by formula.

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

To perform Encryption and Decryption, you must add:

using System.Security.Cryptography; // Namespace

Now take a look at the encryption function:

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;
}

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

Now in the same way, we need to create function for Decrypt the PlainText(Encrypted Text). Have a look at the given function which is responsible for decrypting encrypted text.

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;
        }

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

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

How to Use Encrypt Function

DESCryptoServiceProvider key = new DESCryptoServiceProvider();
// Encrypt a string to a byte array.
 byte[] buffer = Encrypt("ABC", key); // ABC is an example you can put anytext as your need

How to Use Decrypt Function

DESCryptoServiceProvider key = new DESCryptoServiceProvider();
// You can make global variable for holding encrypted byte array value.
byte[] buffer = Encrypt("ABC", key);
string strText = Decrypt(buffer, key);

Download Source Code

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ravi Ranjan Kr
Software Developer
India India
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer

An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.

He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..

He keep himself on the desk of his imagination, hanging around with some inceptions.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
ffernandez2321-Aug-12 3:16
memberffernandez2321-Aug-12 3:16 
GeneralRe: My vote of 4 Pin
RaviRanjankr29-Aug-12 3:47
mvpRaviRanjankr29-Aug-12 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 18 Aug 2012
Article Copyright 2012 by Ravi Ranjan Kr
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid