Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Symmetric Encryption and Decryption in .NET 2.0 using C#

Author: Aadil

In this article I’ll try to explain how you can use the Cryptography classes to encrypt and decrypt data in .NET Framework 2.0

First of all I need to tell you there are two ways to encrypt data one is symmetric encryption and the other is asymmetric encryption. For the context of this article you just need to know that symmetric encryption is used on large amounts of data like if you want to encrypt data coming from some kind of streams, and asymmetric encryption is used to encrypt small amounts of data.

 

I’ll consider symmetric encryption in the article and will explain to encrypt and decrypt textual data from files. To achieve the strongest of encryption there are some very complex algorithms and Microsoft has already done the hard work for us and provided us with this functionality in the form of classes. RijndaelManaged is the class that I am using here that provides the infrastructure for encrypting and decrypting data.

 

Also you need to create a CryptoStream object to get working with the RijndaelManaged class. You need to provide a unique key and initialization vector (IV) each of 16 bytes length, and the same combination should be provided in encrypting and decrypting. Here is the code.

 

Key = Key.Substring(0, 16);

IV = IV.Substring(0, 16);

UTF8Encoding u8e = new UTF8Encoding();

 

  RijndaelManaged rm = new RijndaelManaged();

  CryptoStream cs = new CryptoStream(new FileStream(fileFullPath + ".ENC", FileMode.OpenOrCreate), rm.CreateEncryptor(u8e.GetBytes(Key), u8e.GetBytes(IV)), CryptoStreamMode.Write);

 

What I am doing here is first converting the key and iv to bytes using a utf8 encoder. Then I am creating a new file and providing its stream to the CryptoStream constructor. This CryptoStream is now available and what ever is written to it will be encrypted automatically. Same is the case of you want to decrypt the encrypted file. In this case the stream created will be something like this

 

CryptoStream cs = new CryptoStream(new FileStream(fileFullPath, FileMode.Open), rm.CreateDecryptor(u8e.GetBytes(Key), u8e.GetBytes(IV)), CryptoStreamMode.Read);

 

The rest is just simple stream reading and writing. You can nest the object of CryptoStream with any other stream like a StreamReader or StreamWriter.

Article Comments
sir,this article is very good..i will tell u wot my problem is..im having a authentication form (asp using c#)..while the page is loaded iam creating a random string..when the user enters user name and password ,i hav to encrypt this password using the random string....i expect ur reply soon.

Posted on 4/15/2008 4:32:36 AM by Asif

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Getting information about Drives on a system using the new DriveInfo class in .NET 2.0

Disclaimer - Privacy
© 2002-2017 DevASP.net