Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AesExample
static void Main(string[] args)
{
try
{
const int AesBlockSize = 128;
const CipherMode AesMode = CipherMode.CBC;
const int AesKeySize = 256;
byte[] IV = new byte[] {148, 22, 160, 30, 29, 229, 95, 124, 57, 3, 236, 225, 83, 164, 109, 89};
byte[] key = new byte[] {151, 171, 230, 106, 79, 228, 203, 65, 11, 229, 238, 157, 170, 26, 6, 75, 205, 122, 87, 11, 134, 161, 159, 17, 93, 140, 66, 222, 216, 158, 23, 73 };
string someString = "blahblahblah";
var bytes = Encoding.UTF8.GetBytes(someString);
var aes = new AesManaged()
{
BlockSize = AesBlockSize,
Mode = AesMode,
KeySize = AesKeySize,
IV = IV,
Key = key
};
var memoryStream = new MemoryStream();
int encryptedLen;
using (var transformer = aes.CreateEncryptor())
{
using (var cryptoStream = new CryptoStream(memoryStream, transformer, CryptoStreamMode.Write))
{
cryptoStream.Write(bytes, 0, bytes.Length);
cryptoStream.FlushFinalBlock();
encryptedLen = (int) memoryStream.Length;
cryptoStream.Close();
}
}
var encryptedData = memoryStream.GetBuffer();
for (int i = 0; i < encryptedLen; i++)
{
Console.Write("{0:x2} ", encryptedData[i]);
}
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("press any key");
Console.ReadLine();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.