// (c) 2006 Richard Grimes // www.grimes.demon.co.uk using System; using System.Security.Cryptography; using System.Text; class App { static void Main() { Rijndael r = Rijndael.Create(); r.Mode = CipherMode.ECB; string phrase = "daisy, daisy, give me your answer to"; PasswordDeriveBytes pdb = new PasswordDeriveBytes(phrase, new byte[0]); r.Key = pdb.GetBytes(r.KeySize >> 3); string data = "The quick brown fox jumps over the lazy dog."; ICryptoTransform en = r.CreateEncryptor(); byte[] input = Encoding.ASCII.GetBytes(data); input = CryptoTransform(input, en); Console.WriteLine(BitConverter.ToString(input)); ICryptoTransform de = r.CreateDecryptor(); byte[] output = CryptoTransform(input, de); Console.WriteLine(Encoding.ASCII.GetString(output)); } static byte[] CryptoTransform(byte[] input, ICryptoTransform en) { byte[] output = new byte[input.Length * 2]; int inputOffset = 0; int bytesToRead = en.InputBlockSize; int outputOffset = 0; int totalBytesTransformed = 0; while (true) { int numTransformed = en.TransformBlock(input, inputOffset, bytesToRead, output, outputOffset); inputOffset += bytesToRead; outputOffset += numTransformed; totalBytesTransformed += numTransformed; if (input.Length - inputOffset < en.InputBlockSize) break; } byte[] tempBuffer = en.TransformFinalBlock(input, inputOffset, input.Length - inputOffset); byte[] returnedBuffer = new byte[totalBytesTransformed + tempBuffer.Length]; Array.Copy(output, 0, returnedBuffer, 0, totalBytesTransformed); Array.Copy(tempBuffer, 0, returnedBuffer, totalBytesTransformed, tempBuffer.Length); return returnedBuffer; } }