Sunday, March 24, 2013

Encrypt and Decrypt in C#

This is more fun from C# 4.0 in a Nutshell. This test passes:

using System.IO;
using System.Security.Cryptography;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject
{
   [TestClass]
   public class EncryptionTest
   {
      [TestMethod]
      public void Test()
      {
         byte[] encrytionKey = new byte[16];
         encrytionKey[0] = 0;
         encrytionKey[1] = 255;
         encrytionKey[2] = 77;
         encrytionKey[3] = 42;
         encrytionKey[4] = 8;
         encrytionKey[5] = 88;
         encrytionKey[6] = 188;
         encrytionKey[7] = 205;
         encrytionKey[8] = 254;
         encrytionKey[9] = 38;
         encrytionKey[10] = 49;
         encrytionKey[11] = 213;
         encrytionKey[12] = 13;
         encrytionKey[13] = 89;
         encrytionKey[14] = 57;
         encrytionKey[15] = 67;
         
         byte[] initializationVector = new byte[16];
         initializationVector[0] = 123;
         initializationVector[1] = 18;
         initializationVector[2] = 9;
         initializationVector[3] = 1;
         initializationVector[4] = 8;
         initializationVector[5] = 176;
         initializationVector[6] = 190;
         initializationVector[7] = 167;
         initializationVector[8] = 4;
         initializationVector[9] = 12;
         initializationVector[10] = 54;
         initializationVector[11] = 222;
         initializationVector[12] = 13;
         initializationVector[13] = 98;
         initializationVector[14] = 98;
         initializationVector[15] = 103;
         
         string helloWorld = "Hello World";
         byte[] foo = Encoding.ASCII.GetBytes(helloWorld);
         byte[] bar;
         byte[] baz;
         using (SymmetricAlgorithm fooAlgorithm = Aes.Create())
         {
            using (ICryptoTransform fooEncryptor =
                  fooAlgorithm.CreateEncryptor(encrytionKey, initializationVector))
            {
               using (MemoryStream fooMemory = new MemoryStream())
               {
                  using (Stream fooStream = new CryptoStream(fooMemory, fooEncryptor,
                        CryptoStreamMode.Write))
                  {
                     fooStream.Write(foo, 0, foo.Length);
                  }
                  bar = fooMemory.ToArray();
               }
            }
         }
         Assert.AreEqual(ASCIIEncoding.ASCII.GetString(bar), "v>C??(?c?8@?'\t d");
         using (SymmetricAlgorithm barAlgorithm = Aes.Create())
         {
            using (ICryptoTransform barDecryptor =
                  barAlgorithm.CreateDecryptor(encrytionKey, initializationVector))
            {
               using (MemoryStream barMemory = new MemoryStream())
               {
                  using (Stream barStream = new CryptoStream(barMemory, barDecryptor,
                        CryptoStreamMode.Write))
                  {
                     barStream.Write(bar, 0, bar.Length);
                  }
                  baz = barMemory.ToArray();
               }
            }
         }
         Assert.AreEqual(ASCIIEncoding.ASCII.GetString(baz), "Hello World");
      }
   }
}

No comments:

Post a Comment