Overblog Follow this blog
Edit post Administration Create my blog
Balavardhan Reddy Narani

Data Encryption and Decryption in asp.net

March 7 2009 , Written by Balavardhan Published on #ASP.Net

Data Encryption and Dcryption in C#,  ASP.Net

Required Datnet Namespaces

using System.Security.Cryptography;
using System.IO;
using System.Text;

Function to encrypt String

public static string Encrypt(string TextToBeEncrypted)
RijndaelManaged RijndaelCipher = new RijndaelManaged();
string Password = "CSC";
byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(TextToBeEncrypted);
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
//Creates a symmetric encryptor object.
ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
MemoryStream memoryStream = new MemoryStream();
//Defines a stream that links data streams to cryptographic transformations
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
cryptoStream.Write(PlainText, 0, PlainText.Length);
//Writes the final state and clears the buffer
byte[] CipherBytes = memoryStream.ToArray();
string EncryptedData = Convert.ToBase64String(CipherBytes);
return EncryptedData;


Function to Decrypt String

public static string Decrypt(string TextToBeDecrypted)

RijndaelManaged RijndaelCipher = new RijndaelManaged();
string Password = "CSC";
byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted);
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
//Making of the key for decryption
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
//Creates a symmetric Rijndael decryptor object.
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
MemoryStream memoryStream = new MemoryStream(EncryptedData);
//Defines the cryptographics stream for decryption.THe stream contains decrpted data
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
byte[] PlainText = new byte[EncryptedData.Length];
int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
//Converting to string
string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
return DecryptedData;


Share this post

Repost 0
To be informed of the latest articles, subscribe:

Comment on this post

MBT Womens Amali Shoes 05/20/2011 11:34

 I am devoted to MBT shoes as they instantly relieve some of my back problems. I've tried Shape-ups but they don't provide the support that MBT shoes do. I hope a “slipper” is developed as
I must always have a shoe on due to a foot problem and would like to be able to slide in and out with ease. Are you listening MBT?