示例#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));
            }
        }
示例#2
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));
        }
示例#3
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]);
示例#4
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);
        }
示例#5
0
        public void ReferenceTest()
        {
            var r     = new Random(42);
            var bytes = new byte[DataSize];
            var key   = new byte[32];

            r.NextBytes(bytes);
            r.NextBytes(key);

            var managed = new AesManaged {
                Key = key, Mode = CipherMode.ECB, Padding = PaddingMode.None
            };
            var managedResult = managed.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);

            var niResult = new byte[DataSize];

            Aes.EncryptEcb(bytes, niResult, new Aes256Key(key), PaddingMode.None);

            Assert.Equal(managedResult, niResult);
        }