namespace provides encryption classes that provide most popular symmetric algorithms like
- Data Encryption Standard (DES)
- Triple Data Encryption Standard
- (Rivest Cipher) RC2
- 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. TheDES, 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; // Namespace
Now take a look at 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.
// Close the StreamWriter and CryptoStream.
// Get an array of bytes that represents the memory stream.
byte buffer = ms.ToArray();
// Close the memory stream.
// Return the encrypted byte array.
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)
// 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.
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();
// 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