Exemple #1
0
        public static void AesThrows_PlatformNotSupported_CipherMode_Browser()
        {
            using (Aes aes = Aes.Create())
            {
                Assert.Throws <PlatformNotSupportedException>(() => aes.EncryptEcb(s_plainText, PaddingMode.PKCS7));
                Assert.Throws <PlatformNotSupportedException>(() => aes.EncryptEcb(s_plainText.AsSpan(), PaddingMode.PKCS7));
                Assert.Throws <PlatformNotSupportedException>(() => aes.EncryptEcb(s_plainText.AsSpan(), s_destination, PaddingMode.PKCS7));
                Assert.Throws <PlatformNotSupportedException>(() => aes.DecryptEcb(s_plainText, PaddingMode.PKCS7));
                Assert.Throws <PlatformNotSupportedException>(() => aes.DecryptEcb(s_plainText.AsSpan(), PaddingMode.PKCS7));
                Assert.Throws <PlatformNotSupportedException>(() => aes.DecryptEcb(s_plainText.AsSpan(), s_destination, PaddingMode.PKCS7));

                Assert.Throws <PlatformNotSupportedException>(() => aes.EncryptCfb(s_plainText, s_iv));
                Assert.Throws <PlatformNotSupportedException>(() => aes.EncryptCfb(s_plainText.AsSpan(), s_iv.AsSpan()));
                Assert.Throws <PlatformNotSupportedException>(() => aes.EncryptCfb(s_plainText.AsSpan(), s_iv, s_destination));
                Assert.Throws <PlatformNotSupportedException>(() => aes.DecryptCfb(s_plainText, s_iv));
                Assert.Throws <PlatformNotSupportedException>(() => aes.DecryptCfb(s_plainText.AsSpan(), s_iv.AsSpan()));
                Assert.Throws <PlatformNotSupportedException>(() => aes.DecryptCfb(s_plainText.AsSpan(), s_iv, s_destination));

                aes.Mode = CipherMode.ECB;
                Assert.Throws <PlatformNotSupportedException>(() => aes.CreateEncryptor());
                Assert.Throws <PlatformNotSupportedException>(() => aes.CreateEncryptor(s_iv, s_iv));
                Assert.Throws <PlatformNotSupportedException>(() => aes.CreateDecryptor());
                Assert.Throws <PlatformNotSupportedException>(() => aes.CreateDecryptor(s_iv, s_iv));

                aes.Mode = CipherMode.CFB;
                Assert.Throws <PlatformNotSupportedException>(() => aes.CreateEncryptor());
                Assert.Throws <PlatformNotSupportedException>(() => aes.CreateEncryptor(s_iv, s_iv));
                Assert.Throws <PlatformNotSupportedException>(() => aes.CreateDecryptor());
                Assert.Throws <PlatformNotSupportedException>(() => aes.CreateDecryptor(s_iv, s_iv));
            }
        }
Exemple #2
0
        public void Challenge7_Solution()
        {
            var inputFile = new StreamReader("Assets/inputForChallenge7.txt").ReadToEnd();

            var key = "YELLOW SUBMARINE";

            var decryptedText = Aes.DecryptEcb(Convert.FromBase64String(inputFile), Encoding.UTF8.GetBytes(key));

            Console.WriteLine(Encoding.UTF8.GetString(decryptedText));
        }
Exemple #3
0
        public void Challenge10_InternalTest1()
        {
            var plainText = "abcdefghijklmnop";
            var key       = Encoding.UTF8.GetBytes("YELLOW SUBMARINE");

            var encryptedText = Aes.EncryptEcb(Encoding.UTF8.GetBytes(plainText), key);

            Console.WriteLine($"encryptedText={Convert.ToBase64String(encryptedText)}");

            var decryptedText = Aes.DecryptEcb(encryptedText, key);

            Assert.AreEqual(plainText, Encoding.UTF8.GetString(decryptedText));
        }
Exemple #4
0
        public static void EcbRoundtrip(byte[] plaintext, byte[] ciphertext, PaddingMode padding)
        {
            using (Aes aes = AesFactory.Create())
            {
                aes.Key = s_aes128OneShotKey;

                // Even though we have set the instance to use CFB, the Ecb one shots should
                // always be done in ECB.
                aes.FeedbackSize = 8;
                aes.Mode         = CipherMode.CFB;
                aes.Padding      = padding == PaddingMode.None ? PaddingMode.PKCS7 : PaddingMode.None;

                byte[] encrypted = aes.EncryptEcb(plaintext, padding);
                byte[] decrypted = aes.DecryptEcb(encrypted, padding);

                if (padding == PaddingMode.Zeros)
                {
                    Assert.Equal(plaintext, decrypted[..plaintext.Length]);
Exemple #5
0
        public void SelfTest()
        {
            var r     = new Random(42);
            var plain = new byte[DataSize];
            var key   = new byte[16];

            r.NextBytes(plain);
            r.NextBytes(key);

            var cipher     = new byte[DataSize];
            var plainAgain = new byte[DataSize];
            var k          = new Aes128Key(key);

            Aes.EncryptEcb(plain, cipher, k, PaddingMode.None);
            Aes.DecryptEcb(cipher, plainAgain, k, PaddingMode.None);

            Assert.Equal(plain, plainAgain);
        }