AES encryption and decryption C# - Sample code

3542
Views
1
Answers

Hi,I need to encrypt and decrypt a string using AES encryption method. How can I use it in my C# MVC3 application? Any sample code would be appreciated.

edit | flag
William Patrick
Asked on: Jan 06, 2012 at 2:16AM

1 Answers

94
7
94

AES is a new cryptographic algorithm that can be used to protect electronic data. Specifically, AES is an iterative, symmetric-key block cipher that can use keys of 128, 192, and 256 bits, and encrypts and decrypts data in blocks of 128 bits (16 bytes).
Unlike public-key ciphers, which use a pair of keys, symmetric-key ciphers use the same key to encrypt and decrypt data.
Encrypted data returned by block ciphers have the same number of bits that the input data had. Iterative ciphers use a loop structure that repeatedly performs permutations and substitutions of the input data.
The AES algorithm is based on permutations and substitutions. Permutations are rearrangements of data, and substitutions replace one unit of data with another.

Here is the sample code for encryption and decryption using AES

    // Encryption       

   
//PlainText  = Text to be encrypted

   
//Password  = Password to encrypt with

   
//Salt  = Salt to encrypt with

   
//HashAlgorithm  = Can be either SHA1 or MD5

   
//PasswordIterations  = Number of iterations to do

   
//InitialVector  = Needs to be  ASCII characters long

   
//KeySize  = Can be , , or



   
public static string Encryptstring(string PlainText, string Password,

   
string Salt = "Maddyencrypt", string HashAlgorithm = "MD5",

   
int PasswordIterations = 2, string InitialVector = "ghVNHfg544JUdfjdY5BGVbj67",

   
int KeySize = 256)

   
{

   
if (string.IsNullOrEmpty(PlainText))

   
return "";

   
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);

   
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);

   
byte[] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText);

   
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);

   
byte[] KeyBytes = DerivedPassword.GetBytes(KeySize /8 );

   
RijndaelManaged SymmetricKey = new RijndaelManaged();

   
SymmetricKey.Mode = CipherMode.ECB;

   
byte[] CipherTextBytes = null;

   
using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes))

   
{

   
using (MemoryStream MemStream = new MemoryStream())

   
{

       
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))

       
{

           
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);

           
CryptoStream.FlushFinalBlock();

           
CipherTextBytes = MemStream.ToArray();

           
MemStream.Close();

           
CryptoStream.Close();

       
}

   
}

   
}

   
SymmetricKey.Clear();

   
return Convert.ToBase64String(CipherTextBytes); // returns a encrypted string

   
}





   
// Decryption


   
//CipherText  = Text to be decrypted

   
//Password  = Password to decrypt with

   
//Salt  = Salt to decrypt with

   
//HashAlgorithm  = Can be either SHA1 or MD5

   
//PasswordIterations  = Number of iterations to do

   
//InitialVector  = Needs to be  ASCII characters long

   
//KeySize  = 128, 192, or 256



   
public static string Decryptstring(string CipherText, string Password,

   
string Salt = "Maddyencrypt", string HashAlgorithm = "MD5",

   
int PasswordIterations = 2, string InitialVector = "ghVNHfg544JUdfjdY5BGVbj67",

   
int KeySize = 256)
   
{

   
if (string.IsNullOrEmpty(CipherText))

   
return "";

   
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);

   
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);

   
byte[] CipherTextBytes = Convert.FromBase64String(CipherText);

   
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);

   
byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);

   
RijndaelManaged SymmetricKey = new RijndaelManaged();

   
SymmetricKey.Mode = CipherMode.ECB;

   
byte[] PlainTextBytes = new byte[CipherTextBytes.Length];

   
int ByteCount = 0;

   
using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes))
   
{

   
using (MemoryStream MemStream = new MemoryStream(CipherTextBytes))
   
{

   
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
   
{



       
ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);

       
MemStream.Close();

       
CryptoStream.Close();

   
}

   
}

   
}

   
SymmetricKey.Clear();

   
return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount); // returns decrypted string

   
}
}
edit | flag
Gokul A
Answered on: Jan 06, 2012 at 2:31AM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags