Пример #1
0
        private static void ShowWikipediaTestVectors()
        {
            WriteHeader("Wikipedia Test Vectors Comparison");
            // Test vectors came from http://en.wikipedia.org/wiki/Advanced_Encryption_Standard#Test_vectors on 7 Sep 2009
            byte[] input               = ByteUtilities.GetBytes("4EC137A426DABF8AA0BEB8BC0C2B89D6");
            byte[] aes128Key           = ByteUtilities.GetBytes("95A8EE8E89979B9EFDCBC6EB9797528D");
            byte[] cipheredOutput128   = Aes.Encrypt(input, aes128Key);
            byte[] decipheredOutput128 = Aes.Decrypt(input, aes128Key);

            byte[] wikipediaExpectedCipheredOutput128   = ByteUtilities.GetBytes("D9B65D1232BA0199CDBD487B2A1FD646");
            byte[] wikipediaExpectedDecipheredOutput128 = ByteUtilities.GetBytes("9570C34363565B393503A001C0E23B65");
            ByteUtilities.AssertBytesEqual(wikipediaExpectedCipheredOutput128, cipheredOutput128);
            ByteUtilities.AssertBytesEqual(wikipediaExpectedDecipheredOutput128, decipheredOutput128);

            byte[] aes192Key           = ByteUtilities.GetBytes("95A8EE8E89979B9EFDCBC6EB9797528D432DC26061553818");
            byte[] cipheredOutput192   = Aes.Encrypt(input, aes192Key);
            byte[] decipheredOutput192 = Aes.Decrypt(input, aes192Key);

            byte[] wikipediaExpectedCipheredOutput192   = ByteUtilities.GetBytes("B18BB3E7E10732BE1358443A504DBB49");
            byte[] wikipediaExpectedDecipheredOutput192 = ByteUtilities.GetBytes("29DFD75B85CEE4DE6E26A808CDC2C9C3");

            ByteUtilities.AssertBytesEqual(wikipediaExpectedCipheredOutput192, cipheredOutput192);
            ByteUtilities.AssertBytesEqual(wikipediaExpectedDecipheredOutput192, decipheredOutput192);

            byte[] aes256Key           = ByteUtilities.GetBytes("95A8EE8E89979B9EFDCBC6EB9797528D432DC26061553818EA635EC5D5A7727E");
            byte[] cipheredOutput256   = Aes.Encrypt(input, aes256Key);
            byte[] decipheredOutput256 = Aes.Decrypt(input, aes256Key);

            byte[] wikipediaExpectedCipheredOutput256   = ByteUtilities.GetBytes("2F9CFDDBFFCDE6B9F37EF8E40D512CF4");
            byte[] wikipediaExpectedDecipheredOutput256 = ByteUtilities.GetBytes("110A3545CE49B84BBB7B35236108FA6E");

            ByteUtilities.AssertBytesEqual(wikipediaExpectedCipheredOutput256, cipheredOutput256);
            ByteUtilities.AssertBytesEqual(wikipediaExpectedDecipheredOutput256, decipheredOutput256);
            Console.WriteLine("6 vectors passed");
        }
        private static byte[] PerformRandomizedTest(ICryptoTransform expectedTransform, ICryptoTransform actualTransform,
                                                    byte[] vector)
        {
            using (var msExpected = new MemoryStream())
                using (var msActual = new MemoryStream())
                {
                    using (var csExpected = new CryptoStream(msExpected, expectedTransform, CryptoStreamMode.Write))
                        using (var csActual = new CryptoStream(msActual, actualTransform, CryptoStreamMode.Write))
                        {
                            byte[] bufferToTransform = vector;
                            if (bufferToTransform == null)
                            {
                                int randomBytesToGenerate = _WeakRandom.Next(0, MaxEncryptedSizeInBytes);
                                bufferToTransform = new byte[randomBytesToGenerate];
                                _WeakRandom.NextBytes(bufferToTransform);
                            }
                            csExpected.Write(bufferToTransform, 0, bufferToTransform.Length);
                            csActual.Write(bufferToTransform, 0, bufferToTransform.Length);
                        }

                    byte[] expectedTransformResult = msExpected.ToArray();
                    byte[] actualTransformResult   = msActual.ToArray();

                    ByteUtilities.AssertBytesEqual(expectedTransformResult, actualTransformResult);

                    return(expectedTransformResult);
                }
        }
Пример #3
0
        private static void AssertBytesEqual(byte[] expected, byte[] actual, int bitLength)
        {
            if (bitLength > 1)
            {
                ByteUtilities.AssertBytesEqual(expected, actual);
            }
            else
            {
                // special case CFB test vectors that do 1 bit at a time, just treat as a byte.
                bool actualFirstBit = (actual[0] & 0x80) == 0x80;
                bool expectedBit    = (expected[0] != 0);

                if (actualFirstBit != expectedBit)
                {
                    Debugger.Break();
                    throw new CryptographicException("Bytes were not what was expected");
                }
            }
        }
Пример #4
0
        private static void ShowRijndaelBookTestVectors()
        {
            WriteHeader("Rijndael Book Test");
            Console.WriteLine("Here are the test vectors from Appendix D of \"The Design of Rijndael\" book.");
            Console.WriteLine("(Note how there are more variants than AES allows)");
            Console.WriteLine();
            for (int keyLength = 128; keyLength <= 256; keyLength += 32)
            {
                for (int blockLength = 128; blockLength <= 256; blockLength += 32)
                {
                    Console.WriteLine("block length {0}  key length {1}", blockLength, keyLength);
                    byte[] blockBytes = new byte[blockLength / Constants.BitsPerByte];
                    byte[] keyBytes   = new byte[keyLength / Constants.BitsPerByte];

                    var encrypted = Rijndael.Encrypt(blockBytes, keyBytes);
                    ByteUtilities.WriteBytes(encrypted);
                    var decrypted = Rijndael.Decrypt(encrypted, keyBytes);

                    for (int i = 0; i < decrypted.Length; i++)
                    {
                        if (decrypted[i] != 0)
                        {
                            throw new CryptographicException("The decrypted Rijndael book values were not all zero.");
                        }
                    }

                    var encryptedAgain = Rijndael.Encrypt(encrypted, keyBytes);
                    ByteUtilities.WriteBytes(encryptedAgain);

                    var decryptedAgain = Rijndael.Decrypt(encryptedAgain, keyBytes);
                    ByteUtilities.AssertBytesEqual(decryptedAgain, encrypted);
                    Console.WriteLine();
                }
            }
            Console.WriteLine();
        }