public static byte[] Encrypt(this byte[] data, byte[] key)
{
if (data == null) throw new ArgumentNullException("data");
if (key == null || key.Length != 32) throw new ArgumentException("Key must have 32 length", "key");
byte[] result;
using (var c = Aes.Create())
{
c.Key = key;
c.GenerateIV();
using (var ms = new MemoryStream())
{
ms.Write(c.IV, 0, c.IV.Length);
using (var cs = new CryptoStream(ms, c.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
}
result = ms.ToArray();
}
}
return result;
}