Skip to content

Instantly share code, notes, and snippets.

@smonn
Last active August 29, 2015 13:57
Show Gist options
  • Save smonn/9644803 to your computer and use it in GitHub Desktop.
Save smonn/9644803 to your computer and use it in GitHub Desktop.
Simple class for AES encryption and decryption. Key can be any string, internally it will be converted to an MD5 hash. The IV must be exactly 16 bytes (8 character long string).
using System;
using System.IO;
using System.Security.Cryptography;
public class AesCrypto : ICrypto
{
public const int IV_Length = 16;
private byte[] _key;
private byte[] _iv;
public byte[] Encrypt(string value)
{
byte[] encrypted;
using (var aes = new AesCryptoServiceProvider())
{
aes.Key = _key;
aes.IV = _iv;
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
using (var streamWriter = new StreamWriter(cryptoStream))
streamWriter.Write(value);
encrypted = memoryStream.ToArray();
}
}
return encrypted;
}
public string Decrypt(byte[] value)
{
var encrypted = value;
string decrypted;
using (var aes = new AesCryptoServiceProvider())
{
aes.Key = _key;
aes.IV = _iv;
var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (var memoryStream = new MemoryStream(encrypted))
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
using (var streamReader = new StreamReader(cryptoStream))
decrypted = streamReader.ReadToEnd();
}
return decrypted;
}
private static byte[] GetBytes(string value)
{
var bytes = new byte[value.Length * sizeof(char)];
Buffer.BlockCopy(value.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
private static byte[] MD5(byte[] value)
{
byte[] bytes;
using (var md5 = new MD5CryptoServiceProvider())
bytes = md5.ComputeHash(value);
return bytes;
}
public void Configure(string opt1, string opt2)
{
_key = MD5(GetBytes(opt1));
_iv = Convert.FromBase64String(opt2);
if (_iv.Length != IV_Length)
throw new ArgumentException("IV must be 16 bytes long", "iv");
}
}
public interface ICrypto
{
void Configure(string opt1, string opt2);
byte[] Encrypt(string value);
string Decrypt(byte[] value);
}
public interface IInitVectorGenerator
{
byte[] Generate(int length);
}
using System.Security.Cryptography;
public class RNGInitVectorGenerator : IInitVectorGenerator
{
public byte[] Generate(int length)
{
using (var rng = new RNGCryptoServiceProvider())
{
var bytes = new byte[length];
rng.GetBytes(bytes);
return bytes;
}
}
}
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Serialization;
public class RsaCrypto : ICrypto
{
public const int KeyLength = 2048;
private RSAParameters _public;
private RSAParameters _private;
public void Configure(string opt1, string opt2)
{
_public = GetParams(opt1);
_private = GetParams(opt2);
}
public byte[] Encrypt(string value)
{
using (var rsa = new RSACryptoServiceProvider(KeyLength))
{
rsa.ImportParameters(_public);
var bytes = ToBytes(value);
return rsa.Encrypt(bytes, false);
}
}
public string Decrypt(byte[] value)
{
using (var rsa = new RSACryptoServiceProvider(KeyLength))
{
rsa.ImportParameters(_private);
var bytes = rsa.Decrypt(value, false);
return ToString(bytes);
}
}
private string ToString(byte[] bytes)
{
return Encoding.Unicode.GetString(bytes);
}
private RSAParameters GetParams(string keyString)
{
using (var reader = new StringReader(keyString))
{
var serializer = new XmlSerializer(typeof(RSAParameters));
return (RSAParameters)serializer.Deserialize(reader);
}
}
private byte[] ToBytes(string value)
{
return Encoding.Unicode.GetBytes(value);
}
}
@smonn
Copy link
Author

smonn commented Mar 19, 2014

Usage:

var rng = new RNGInitVectorGenerator();
var key = "my top secret key";
var iv = rng.Generate(AesCrypto.IV_Length);
var crypto = new AesCrypto();
crypto.Configure(key, Convert.ToBase64String(iv));
var encrypted = crypto.Encrypt("some secret");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment