Encryption Decryption Using AES
The Advanced Encryption Standard (AES) is a specification for the encryption of electronic data established by the U.S. National Institute of Standards and Technology (NIST) in 2001.
AES is based on the Rijndael cipher developed by two Belgian cryptographers, Joan Daemen and Vincent Rijmen, who submitted a proposal to NIST during the AES selection process.Rijndael is a family of ciphers with different key and block sizes.
For AES, NIST selected three members of the Rijndael family, each with a block size of 128 bits, but three different key lengths: 128, 192 and 256 bits.
AES has been adopted by the U.S. government and is now used worldwide. It supersedes the Data Encryption Standard (DES), which was published in 1977. The algorithm described by AES is a symmetric-key algorithm, meaning the same key is used for both encrypting and decrypting the data.
In the United States, AES was announced by the NIST as U.S. FIPS PUB 197 (FIPS 197) on November 26, 2001. This announcement followed a five-year standardization process in which fifteen competing designs were presented and evaluated, before the Rijndael cipher was selected as the most suitable (see Advanced Encryption Standard process for more details).
AES became effective as a federal government standard on May 26, 2002 after approval by the Secretary of Commerce. AES is included in the ISO/IEC 18033-3 standard. AES is available in many different encryption packages, and is the first publicly accessible and open cipher approved by the National Security Agency (NSA) for top secret information when used in an NSA approved cryptographic module (see Security of AES, below). (From wikipedia)
Class AES
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace Encryption_Decryption_Using_AES
{
class AES
{
byte[] key;
byte[] iVector;
public byte[] Key
{
get
{
return key;
}
}
public byte[] IV
{
get
{
return iVector;
}
}
public AES()
{
RijndaelManaged rij = new RijndaelManaged();
rij.GenerateKey();
rij.GenerateIV();
key = rij.Key;
iVector = rij.IV;
}
public AES(byte[] key, byte[] iv)
{
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("IV");
this.key = key;
this.iVector = iv;
}
private byte[] EncryptText(string plainText)
{
if (plainText == "" || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
byte[] cipherBytes;
using (RijndaelManaged objRM = new RijndaelManaged())
{
objRM.Key = key;
objRM.IV = iVector;
ICryptoTransform encryptor = objRM.CreateEncryptor(objRM.Key, objRM.IV);
using (MemoryStream objMS = new MemoryStream())
{
using (CryptoStream objCS = new CryptoStream(objMS, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter objSW = new StreamWriter(objCS))
{
objSW.Write(plainText);
}
cipherBytes = objMS.ToArray();
}
}
}
return cipherBytes;
}
private string DecryptBytes(Byte[] cipherBytes)
{
if (cipherBytes == null || cipherBytes.Length <= 0)
throw new ArgumentNullException("cipherBytes");
string plainText = "";
using (RijndaelManaged objRM = new RijndaelManaged())
{
objRM.Key = key;
objRM.IV = iVector;
ICryptoTransform decryptor = objRM.CreateDecryptor(objRM.Key, objRM.IV);
using (MemoryStream objMS = new MemoryStream(cipherBytes))
{
using (CryptoStream objCS = new CryptoStream(objMS, decryptor, CryptoStreamMode.Read))
{
using (StreamReader objSR = new StreamReader(objCS))
{
plainText = objSR.ReadToEnd();
}
}
}
}
return plainText;
}
public string AESEncryption(string plainText)
{
if (plainText == "" || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
byte[] cipherBytes = EncryptText(plainText);
StringBuilder cipherText = new StringBuilder(cipherBytes.Length);
foreach (byte b in cipherBytes)
cipherText.Append(b.ToString("X2"));
return cipherText.ToString();
}
public string AESDecryption(string cipherText)
{
if (cipherText == "" || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
byte[] b = new byte[cipherText.Length / 2];
string[] hexValues = new string[cipherText.Length / 2];
for (int i = 0, j = 0; i < cipherText.Length / 2; j += 2)
{
hexValues[i] = cipherText.Substring(j, 2);
i++;
}
for (int i = 0; i < hexValues.Length; i++)
{
b[i] = Convert.ToByte(hexValues[i], 16);
}
return DecryptBytes(b);
}
}
}
Use it :
UAES = new AES(); //Encryption crypt =
UAES
.AESEncryption(origine)
//
Decryption
origine
=
UAES
.AESEncryption(
)
crypt