private static void RunTests(IEnumerable<string[]> tests, AESCipher aes)
        {
            foreach (var test in tests)
            {
                var iv = test[0];
                var plaintext = test[1];
                var ciphertext = test[2];

                var cbc = new CBCBlockCipher(aes);
                var cipher = new BlockCipherAdapter(cbc);

                // encryption
                var plainInput = HexConverter.FromHex(plaintext);
                var actual = new byte[plainInput.Length];

                cipher.Init(new IVParameter(null, HexConverter.FromHex(iv)));
                cipher.Encrypt(plainInput, 0, actual, 0, plainInput.Length);

                Assert.AreEqual(ciphertext, HexConverter.ToHex(actual));



                // decryption
                var cipherInput = HexConverter.FromHex(ciphertext);
                actual = new byte[cipherInput.Length];

                cipher.Init(new IVParameter(null, HexConverter.FromHex(iv)));
                cipher.Decrypt(cipherInput, 0, actual, 0, cipherInput.Length);

                Assert.AreEqual(plaintext, HexConverter.ToHex(actual));
            }
        }
        public void AESCBC128Test()
        {
            var aes = new AESCipher(128);
            aes.Init(new KeyParameter(HexConverter.FromHex("2b7e151628aed2a6abf7158809cf4f3c")));

            var tests = new[]
            {
                new[] { "000102030405060708090a0b0c0d0e0f", "6bc1bee22e409f96e93d7e117393172a6bc1bee22e409f96e93d7e117393172a", "7649abac8119b246cee98e9b12e9197d4cbbc858756b358125529e9698a38f44"},
            };

            RunTests(tests, aes);
        }
Beispiel #3
0
        public void TestAES256()
        {
            var tests = new[]
            {
                new [] { "6bc1bee22e409f96e93d7e117393172a", "f3eed1bdb5d2a03c064b5a7e3db181f8" },
                new [] { "ae2d8a571e03ac9c9eb76fac45af8e51", "591ccb10d410ed26dc5ba74a31362870" },
                new [] { "30c81c46a35ce411e5fbc1191a0a52ef", "b6ed21b99ca6f4f9f153e7b1beafed1d" },
                new [] { "f69f2445df4f9b17ad2b417be66c3710", "23304b7a39f9f3ff067d8d8f9e24ecc7" },
            };

            var aes = new AESCipher(256);
            aes.Init(new KeyParameter(HexConverter.FromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")));

            RunAESTests(tests, aes);
        }
Beispiel #4
0
        public void TestAES192()
        {
            var tests = new[]
            {
                new [] { "6bc1bee22e409f96e93d7e117393172a", "bd334f1d6e45f25ff712a214571fa5cc" },
                new [] { "ae2d8a571e03ac9c9eb76fac45af8e51", "974104846d0ad3ad7734ecb3ecee4eef" },
                new [] { "30c81c46a35ce411e5fbc1191a0a52ef", "ef7afd2270e2e60adce0ba2face6444e" },
                new [] { "f69f2445df4f9b17ad2b417be66c3710", "9a4b41ba738d6c72fb16691603c18e0e" },
            };

            var aes = new AESCipher(192);
            aes.Init(new KeyParameter(HexConverter.FromHex("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b")));

            RunAESTests(tests, aes);
        }
Beispiel #5
0
        public void TestAES128()
        {
            var tests = new[]
            {
                new [] { "6bc1bee22e409f96e93d7e117393172a", "3ad77bb40d7a3660a89ecaf32466ef97" },
                new [] { "ae2d8a571e03ac9c9eb76fac45af8e51", "f5d3d58503b9699de785895a96fdbaaf" },
                new [] { "30c81c46a35ce411e5fbc1191a0a52ef", "43b1cd7f598ece23881b00e3ed030688" },
                new [] { "f69f2445df4f9b17ad2b417be66c3710", "7b0c785e27e8ad3f8223207104725dd4" },
            };

            var aes = new AESCipher(128);
            aes.Init(new KeyParameter(HexConverter.FromHex("2b7e151628aed2a6abf7158809cf4f3c")));

            RunAESTests(tests, aes);
        }
Beispiel #6
0
        public void TestAES128()
        {
            var key = "ad7a2bd03eac835a6f620fdcb506b345";
            var iv = "12153524c0895e81b2c28465";
            var aad = "d609b1f056637a0d46df998d88e52e00b2c2846512153524c0895e81";

            var plaintext = "08000f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a0002";
            var ciphertext = "701afa1cc039c0d765128a665dab69243899bf7318ccdc81c9931da17fbe8edd7d17cb8b4c26fc81e3284f2b7fba713d";
            var tag = "4f8d55e7d3f06fd5a13c0c29b9d5b880";

            var aes = new AESCipher(128);
            var gcm = new GCMCipher(aes);

            // encryption
            gcm.Init(new AADParameter(new IVParameter(new KeyParameter(HexConverter.FromHex(key)), HexConverter.FromHex(iv)), HexConverter.FromHex(aad)));

            var encryptInput = HexConverter.FromHex(plaintext);
            var encryptOutput = new byte[encryptInput.Length];
            var encryptTag = new byte[16];

            gcm.Encrypt(encryptInput, 0, encryptOutput, 0, encryptInput.Length);
            gcm.EncryptFinal(encryptTag, 0);

            Assert.AreEqual(ciphertext, HexConverter.ToHex(encryptOutput));
            Assert.AreEqual(tag, HexConverter.ToHex(encryptTag));

            // decryption
            gcm.Init(new AADParameter(new IVParameter(new KeyParameter(HexConverter.FromHex(key)), HexConverter.FromHex(iv)), HexConverter.FromHex(aad)));

            var decryptInput = HexConverter.FromHex(ciphertext + tag);
            var decryptOutput = new byte[decryptInput.Length - 16];

            var offset = gcm.Decrypt(decryptInput, 0, decryptOutput, 0, decryptInput.Length);
            gcm.DecryptFinal(decryptInput, offset, decryptOutput, decryptInput.Length);

            Assert.AreEqual(plaintext, HexConverter.ToHex(decryptOutput));
        }