This C# utility class will help you encrypt and decrypt data with AES encryption
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace SouthDataAPI.Utils
{
public class AESEncryptionUtils
{
private static byte[] EncryptionSalt = new byte[]
{
0x67, 0x95, 0x52, 0xc7, 0x1e, 0x38, 0xe3, 0x75, 0x8d, 0x5e, 0x37, 0x39, 0x87
};
public static string Encrypt(string plainText, string password)
{
if (plainText == null)
throw new ArgumentNullException("plainText");
if (password == null)
throw new ArgumentNullException("password");
// Will return the cipher text
string cipherText = "";
byte[] salt = EncryptionSalt;
// Convert plain text to bytes
byte[] plainBytes = Encoding.Unicode.GetBytes(plainText);
// create new password derived bytes using password/salt
using (Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, salt))
{
using (Aes aes = AesManaged.Create())
{
// Generate key and iv from password/salt and pass to aes
aes.Key = pdb.GetBytes(aes.KeySize / 8);
aes.IV = pdb.GetBytes(aes.BlockSize / 8);
// Open a new memory stream to write the encrypted data to
using (MemoryStream ms = new MemoryStream())
{
// Create a crypto stream to perform encryption
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
// write encrypted bytes to memory
cs.Write(plainBytes, 0, plainBytes.Length);
}
// get the cipher bytes from memory
byte[] cipherBytes = ms.ToArray();
// create a new byte array to hold salt + cipher
byte[] saltedCipherBytes = new byte[salt.Length + cipherBytes.Length];
// copy salt + cipher to new array
Array.Copy(salt, 0, saltedCipherBytes, 0, salt.Length);
Array.Copy(cipherBytes, 0, saltedCipherBytes, salt.Length, cipherBytes.Length);
// convert cipher array to base 64 string
cipherText = Convert.ToBase64String(saltedCipherBytes);
}
aes.Clear();
}
}
return cipherText;
}
public static string Decrypt(string cipherText, string password)
{
if (cipherText == null)
throw new ArgumentNullException("cipherText");
if (password == null)
throw new ArgumentNullException("password");
// will return plain text
string plainText = "";
// get salted cipher array
byte[] saltedCipherBytes = Convert.FromBase64String(cipherText);
// create array to hold salt
byte[] salt = EncryptionSalt;
// create array to hold cipher
byte[] cipherBytes = new byte[saltedCipherBytes.Length - salt.Length];
// copy salt/cipher to arrays
Array.Copy(saltedCipherBytes, 0, salt, 0, salt.Length);
Array.Copy(saltedCipherBytes, salt.Length, cipherBytes, 0, saltedCipherBytes.Length - salt.Length);
// create new password derived bytes using password/salt
using (Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, salt))
{
using (Aes aes = AesManaged.Create())
{
// Generate key and iv from password/salt and pass to aes
aes.Key = pdb.GetBytes(aes.KeySize / 8);
aes.IV = pdb.GetBytes(aes.BlockSize / 8);
// Open a new memory stream to write the encrypted data to
using (MemoryStream ms = new MemoryStream())
{
// Create a crypto stream to perform decryption
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
// write decrypted data to memory
cs.Write(cipherBytes, 0, cipherBytes.Length);
}
// convert decrypted array to plain text string
plainText = Encoding.Unicode.GetString(ms.ToArray());
}
aes.Clear();
}
}
return plainText;
}
#region Decrypt File
private static byte[] fileEncryptionSalt = new byte[]
{
0xa4, 0x19, 0x62, 0x45, 0xda, 0xb8, 0x99, 0x33, 0x99, 0x65, 0x55, 0x98, 0x0d
};
public static byte[] DecryptFile(byte[] input, string encryptionKey)
{
byte[] returnVal;
using (var encryptor = Aes.Create())
{
var pdb = new Rfc2898DeriveBytes(encryptionKey, fileEncryptionSalt);
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (var inputStream = new MemoryStream(input))
{
using (var cs = new CryptoStream(inputStream, encryptor.CreateDecryptor(), CryptoStreamMode.Read))
{
returnVal = new byte[input.Length];
try
{
cs.Read(returnVal, 0, returnVal.Length);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
return returnVal;
}
#endregion
}
}