Last active
August 29, 2015 14:05
-
-
Save trcio/cbb3204079db2abf1a47 to your computer and use it in GitHub Desktop.
Simple AES wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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