Skip to content

Instantly share code, notes, and snippets.

@trcio
Last active August 29, 2015 14:05
Show Gist options
  • Save trcio/cbb3204079db2abf1a47 to your computer and use it in GitHub Desktop.
Save trcio/cbb3204079db2abf1a47 to your computer and use it in GitHub Desktop.
Simple AES wrapper
using System.Security.Cryptography;
public static class Aes
{
public static byte[] Encrypt(byte[] data, byte[] key)
{
if (data == null)
throw new ArgumentNullException("data");
if (key == null)
throw new ArgumentNullException("key");
using (var aes = new RijndaelManaged {Key = GetKey(key), Mode = CipherMode.ECB})
{
var enc = aes.CreateEncryptor();
return enc.TransformFinalBlock(data, 0, data.Length);
}
}
public static byte[] Decrypt(byte[] data, byte[] key)
{
if (data == null)
throw new ArgumentNullException("data");
if (key == null)
throw new ArgumentNullException("key");
using (var aes = new RijndaelManaged {Key = GetKey(key), Mode = CipherMode.ECB})
{
var enc = aes.CreateDecryptor();
return enc.TransformFinalBlock(data, 0, data.Length);
}
}
private static byte[] GetKey(byte[] key)
{
using (var provider = new SHA256CryptoServiceProvider())
{
return provider.ComputeHash(key);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment