Skip to content

Instantly share code, notes, and snippets.

@yutopio
Last active August 29, 2015 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yutopio/be41941ae99104d822de to your computer and use it in GitHub Desktop.
Save yutopio/be41941ae99104d822de to your computer and use it in GitHub Desktop.
AES Cryptography Sample
using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
class Program
{
static void Main(string[] args)
{
string iv, key;
const string msg = "Hello world!";
byte[] buffer;
using (var aes = new AesManaged())
{
aes.GenerateIV();
aes.GenerateKey();
iv = Convert.ToBase64String(aes.IV);
key = Convert.ToBase64String(aes.Key);
using (var enc = aes.CreateEncryptor())
{
buffer = Encoding.ASCII.GetBytes(msg);
buffer = enc.TransformFinalBlock(buffer, 0, buffer.Length);
}
}
using (var aes = new AesManaged())
{
aes.IV = Convert.FromBase64String(iv);
aes.Key = Convert.FromBase64String(key);
using (var dec = aes.CreateDecryptor())
{
buffer = dec.TransformFinalBlock(buffer, 0, buffer.Length);
Debug.Assert(Encoding.ASCII.GetString(buffer) == msg);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment