using System.Security.Cryptography;
class DataCryptor : IDisposable {
static byte[] DefaultKey = {
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
static byte[] DefaultIV = {
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
public byte[] Encrypt(byte[] b, byte[] key, byte[] iv) {
if (b == null || b.Length == 0) throw new ArgumentException();
if (key == null || key.Length == 0) key = DefaultKey;
if (iv == null || iv.Length == 0) iv = DefaultIV;
ICryptoTransform transform = aes.CreateEncryptor();
MemoryStream result = new MemoryStream();
using (CryptoStream cs = new CryptoStream(result, transform, CryptoStreamMode.Write)) {
while ( ofs < b.Length ) {
if (c > bufsize) c = bufsize;
public byte[] Decrypt(byte[] b, byte[] key, byte[] iv) {
public static void Main() {
DataCryptor dc = new DataCryptor();
byte[] data = Encoding.ASCII.GetBytes("Encryption example in C# Application");
Console.WriteLine("Before Encrypt");
Console.WriteLine(" Raw : {0}", Encoding.ASCII.GetString(data));
Console.WriteLine(" Base64: {0}", Convert.ToBase64String(data));
data = dc.Encrypt(data, null, null);
Console.WriteLine("After Encrypt");
Console.WriteLine(" Raw : {0}", Encoding.ASCII.GetString(data));
Console.WriteLine(" Base64: {0}", Convert.ToBase64String(data));