Welcome :Guest

Congratulations!!!

Top 5 Contributors of the Month
Shivakumaran
hdjk11
dfefde5
hoangnganvy1993

 Home >> Articles >> Security >> Post New Resource

# Cryptography : Symmetric Encryption by Symmetric Algorithm Classes Ã¢â‚¬â€œ Part 2

Posted By:Ravi Ranjan Kumar       Posted Date: August 16, 2012    Points: 200    Category: Security    URL: http://www.dotnetspark.com

This article enable you to understand the basic codes for encrypt and decrypt by using Symmetric Classes.

In previous blog Ã¢â‚¬â€œ [ 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, now In addition to previous blog here we will learn about TripleDES class, RC2 class and Rijndael class.

## TripleDES class

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

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

Note SourceCode was almost same as we have read in previous article. Just Implantation are different so keep focus on Implementation code.

To perform Encryption and Decryption. you must add

 `1` `using` `System.Security.Cryptography; ``// Namespace`

Now take a look at encryption function

 `01` `public` `static` `byte``[] Encrypt(``string` `strText, SymmetricAlgorithm key)`
 `02` `{`
 `03` `// Create a memory stream.`
 `04` `MemoryStream ms = ``new` `MemoryStream();`
 `05` `// Create a CryptoStream using the memory stream and the`
 `06` `// CSP(cryptoserviceprovider) DES key.`
 `07` `CryptoStream crypstream = ``new` `CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);`
 `08` `// Create a StreamWriter to write a string to the stream.`
 `09` `StreamWriter sw = ``new` `StreamWriter(crypstream);`
 `10` `// Write the strText to the stream.`
 `11` `sw.WriteLine(strText);`
 `12` `// Close the StreamWriter and CryptoStream.`
 `13` `sw.Close();`
 `14` `crypstream.Close();`
 `15` `// Get an array of bytes that represents the memory stream.`
 `16` `byte``[] buffer = ms.ToArray();`
 `17` `// Close the memory stream.`
 `18` `ms.Close();`
 `19`
 `20` `// Return the encrypted byte array.`
 `21` `return` `buffer;`
 `22` `}`

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.

 `01` `public` `static` `string` `Decrypt(``byte``[] encryptText, SymmetricAlgorithm key)`
 `02`
 `03` `        ``{`
 `04` `            ``// Create a memory stream to the passed buffer.`
 `05` `            ``MemoryStream ms = ``new` `MemoryStream(encryptText);`
 `06` ` ``// Create a CryptoStream using  memory stream and CSP DES key.`
 `07` `            ``CryptoStream crypstream = ``new` `CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);`
 `08` `         ``// Create a StreamReader for reading the stream.`
 `09` `            ``StreamReader sr = ``new` `StreamReader(crypstream);`
 `10` `      ``// Read the stream as a string.`
 `11` `          ``string` `val = sr.ReadLine();`
 `12` `           ``// Close the streams.`
 `13` `            ``sr.Close();`
 `14` `            ``crypstream.Close();`
 `15` `            ``ms.Close();`
 `16` `          ``return` `val;`
 `17` `        ``}`

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.

Note : we need to access TripleDESCryptoServiceProvider class here.

 `1` `TripleDESCryptoServiceProvider key = ``new` `TripleDESCryptoServiceProvider(); ``// Key is the object`

How to use Encrypt Function

 `1` `//Creating object of TripleDES class.`
 `2` `TripleDESCryptoServiceProvider key = ``new` `TripleDESCryptoServiceProvider();`
 `3` `// Encrypt a string to a byte array.`
 `4` ` ``byte``[] buffer = Encrypt(``"ABC"``, key); ``// ABC is an example you can put anytext as your need`

How to use Decrypt Function

 `1` `//Creating object of TripleDES class.`
 `2` `TripleDESCryptoServiceProvider key = ``new` `TripleDESCryptoServiceProvider();`
 `3` `// You can make global variable for holding encrypted byte array value.`
 `4` `byte``[] buffer = Encrypt(``"ABC"``, key);`
 `5` `string` `strText = Decrypt(buffer, key); ``// strText will hold the original text back.`

## RC2 class

The RC2 classis an abstract class that extends the Symmetric Algorithm class and provides support for the RC2 algorithm.
Ron Rivest He is the one who discovered RC2, RC4, RC5, and RC6. Hence RC stands for RonÃ¢â‚¬â„¢s Codes. however Lotus Software Company is the backend in creating the whole script of RC2 algorithm.

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.
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 RC2CryptoServiceProvider class is the concrete RC2 algorithm class and it extends the RC2 class. The RC2CryptoServiceProvider 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.

Note : we need to access RC2CryptoServiceProvider class here.

 `1` `RC2CryptoServiceProvider key = ``new` `RC2CryptoServiceProvider();`

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.

How to use Encrypt Function

 `1` `//Creating object of RC2 class.`
 `2` `RC2CryptoServiceProvider key = ``new` `RC2CryptoServiceProvider();`
 `3` `// Encrypt a string to a byte array.`
 `4` ` ``byte``[] buffer = Encrypt(``"ABC"``, key); ``// ABC is an example you can put anytext as your need`

How to use Decrypt Function

 `1` `//Creating object of RC2 class.`
 `2` `RC2CryptoServiceProvider key = ``new` `RC2CryptoServiceProvider();`
 `3` `// You can make global variable for holding encrypted byte array value.`
 `4` `byte``[] buffer = Encrypt(``"ABC"``, key);`
 `5` `string` `strText = Decrypt(buffer, key); ``// strText will hold the original text back.`

## Rijndael Class

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

As we have read It is considered to be the replacement for the DES algorithm and was adopted as a Federal Information Processing Standard(FIPS) standard in 2001. The RijndaelManaged 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.
In .NetFramework Rijndael algorithm supports a fixed encryption key size of 128 bits, 192 bits, 192 bits or 256 bits as 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
The Rijndael class is the predecessor of the Aes algorithm. You should use the Aes algorithm instead of Rijndael.

Note : we need to access RijndaelManaged class here.

 `1` `RijndaelManaged key = ``new` `RijndaelManaged();`

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 RijndaelManaged for Rijndael encryption.

How to use Encrypt Function

 `1` `//Creating object of RijndaelManaged class.`
 `2` `RijndaelManaged key = ``new` `RijndaelManaged();`
 `3` `// Encrypt a string to a byte array.`
 `4` ` ``byte``[] buffer = Encrypt(``"ABC"``, key); ``// ABC is an example you can put anytext as your need`

How to use Decrypt Function

 `1` `//Creating object of RijndaelManaged class.`
 `2` `RijndaelManaged key = ``new` `RijndaelManaged();`
 `3` `// You can make global variable for holding encrypted byte array value.`
 `4` `byte``[] buffer = Encrypt(``"ABC"``, key);`
 `5` `string` `strText = Decrypt(buffer, key); ``// strText will hold the original text back.`

Look at given below for output by using different Symmetric Algorithm applied on same plain text (eg : ABC)

Responses

No response found. Be the first to respond this post

Post Comment