Beispiel #1
0
        public static byte[] CTR128(byte[] block, byte[] key, byte[] ctr)
        {
            var aes = new AesCtr(ctr);

            var output = new byte[block.Length];

            aes.CreateDecryptor(key).TransformBlock(block, 0, block.Length, output, 0);
            return(output);
        }
Beispiel #2
0
 public static void Test()
 {
     // Taken from http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
     // F.5.1 CTR-AES128.Encrypt and
     // F.5.2 CTR-AES128.Decrypt
     string[] keys = new[]
     {
         "2b7e151628aed2a6abf7158809cf4f3c",
         "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
         "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
     };
     string[] plains = new[]
     {
         "6bc1bee22e409f96e93d7e117393172a",
         "ae2d8a571e03ac9c9eb76fac45af8e51",
         "30c81c46a35ce411e5fbc1191a0a52ef",
         "f69f2445df4f9b17ad2b417be66c3710",
     };
     string[][] encrypteds = new[]
     {
         new[]
         {
             "874d6191b620e3261bef6864990db6ce",
             "9806f66b7970fdff8617187bb9fffdff",
             "5ae4df3edbd5d35e5b4f09020db03eab",
             "1e031dda2fbe03d1792170a0f3009cee",
         },
         new[]
         {
             "1abc932417521ca24f2b0459fe7e6e0b",
             "090339ec0aa6faefd5ccc2c6f4ce8e94",
             "1e36b26bd1ebc670d1bd1d665620abf7",
             "4f78a7f6d29809585a97daec58c6b050",
         },
         new[]
         {
             "601ec313775789a5b7a7f504bbf3d228",
             "f443e3ca4d62b59aca84e990cacaf5c5",
             "2b0930daa23de94ce87017ba2d84988d",
             "dfc9c58db67aada613c2dd08457941a6",
         },
     };
     for (int i = 0; i < keys.Length; i++)
     {
         var aes = new AesCtr();
         aes.Key = GetBytes(keys[i]);
         aes.IV  = GetBytes("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
         Console.WriteLine("{0} bits", aes.KeySize);
         {
             Console.WriteLine("Encrypt");
             ICryptoTransform encryptor = aes.CreateEncryptor();
             var cipher = new byte[16];
             for (int j = 0; j < plains.Length; j++)
             {
                 byte[] plain = GetBytes(plains[j]);
                 encryptor.TransformBlock(plain, 0, plain.Length, cipher, 0);
                 string cipherHex = BitConverter.ToString(cipher).Replace("-", string.Empty).ToLowerInvariant();
                 if (cipherHex != encrypteds[i][j])
                 {
                     throw new Exception("Error encrypting " + j);
                 }
                 Console.WriteLine(cipherHex);
             }
         }
         Console.WriteLine();
         {
             Console.WriteLine("Decrypt");
             ICryptoTransform decryptor = aes.CreateDecryptor();
             var plain = new byte[16];
             for (int j = 0; j < encrypteds[i].Length; j++)
             {
                 byte[] encrypted = GetBytes(encrypteds[i][j]);
                 decryptor.TransformBlock(encrypted, 0, encrypted.Length, plain, 0);
                 string plainHex = BitConverter.ToString(plain).Replace("-", string.Empty).ToLowerInvariant();
                 if (plainHex != plains[j])
                 {
                     throw new Exception("Error decrypting " + j);
                 }
                 Console.WriteLine(plainHex);
             }
         }
         Console.WriteLine();
     }
 }