public void Test_Encrypt_EncryptsPlaintext()
        {
            strategy = new AesEncryptionStrategy(128, 1000, "06DC30A48ADEEE72D98E33C2CEAEAD3E", "ED124530AF64A5CAD8EF463CF5628434", "password");

            string ciphertext = strategy.Encrypt("Hello world");

            Assert.Equal("A/DzjV17WVS6ZAKsLOaC/Q==", ciphertext);
        }
Exemple #2
0
        public static IEnumerable <object[]> PrivateKeys()
        {
            yield return(new object[] { AesEncryptionStrategy.GeneratePrivateKey() });

            yield return(new object[] { Convert.FromBase64String("vibaOtNImyzWYyqdkWm3LsX53dg9fp+gMrhd8vbxAw8=") });

            yield return(new object[] { Convert.FromBase64String("ElOwc3Xtni8vxtDyYO2vMPPcgjT71NSjKDcVVseBCqM=") });
        }
        public void Test_Decrypt_DecryptsTheCiphertext_WhenTheSameStrategyInstanceIsUsed()
        {
            strategy = new AesEncryptionStrategy(128, 1000, "password");

            string ciphertext = strategy.Encrypt("Hello world");

            Assert.Equal("Hello world", strategy.Decrypt(ciphertext));
        }
        public static IEnumerable <object[]> ValidPrivateKeys()
        {
            yield return(new object[] { Convert.FromBase64String("vibaOtNImyzWYyqdkWm3LsX53dg9fp+gMrhd8vbxAw8=") });

            yield return(new object[] { AesEncryptionStrategy.GeneratePrivateKey() });

            yield return(new object[] { AesEncryptionStrategy.GeneratePrivateKey() });
        }
        public void Test_Decrypt_DecryptsTheCiphertext_WhenTheSameConfigurationIsUsed()
        {
            strategy = new AesEncryptionStrategy(128, 1000, "password");

            string ciphertext = strategy.Encrypt("Hello world");

            strategy = new AesEncryptionStrategy(strategy.KeySize, strategy.IterationCount, strategy.Salt, strategy.Iv, "password");
            Assert.Equal("Hello world", strategy.Decrypt(ciphertext));
        }
        public void TestAesEncryptionStrategy1(byte[] privateKey)
        {
            try
            {
                var strategy = new AesEncryptionStrategy(privateKey);

                throw new Exception("Test did not throw expected exception!");
            }
            catch (ArgumentException ex)
            {
                ex.Should().NotBeNull();
            }
        }
        public void Test_Decrypt_DoesNotDecryptTheCiphertext_WhenTheIncorrectSaltIsUsed()
        {
            strategy = new AesEncryptionStrategy(128, 1000, "password");
            string ciphertext = strategy.Encrypt("Hello world");

            strategy = new AesEncryptionStrategy(strategy.KeySize, strategy.IterationCount, "133D30C2A658B3081279A97FD3B1F7CDE10C4FB61D39EEA8", strategy.Iv, "password");

            try
            {
                strategy.Decrypt(ciphertext);
                throw new TestFailedException();
            }
            catch (CryptographicException ce)
            {
                // this is expected
            }
        }
        public void Test_Decrypt_DoesNotDecryptTheCiphertext_WhenTheIncorrectIterationCountIsUsed()
        {
            strategy = new AesEncryptionStrategy(128, 1000, "password");
            string ciphertext = strategy.Encrypt("Hello world");

            strategy = new AesEncryptionStrategy(strategy.KeySize, 2000, strategy.Salt, strategy.Iv, "password");

            try
            {
                strategy.Decrypt(ciphertext);
                throw new TestFailedException();
            }
            catch (CryptographicException ce)
            {
                // this is expected
            }
        }
        public void test_decrypt_doesNotDecryptTheCiphertext_WhenTheIncorrectPassphraseIsUsed()
        {
            strategy = new AesEncryptionStrategy(128, 1000, "password");
            string ciphertext = strategy.Encrypt("Hello world");

            strategy = new AesEncryptionStrategy(strategy.KeySize, strategy.IterationCount, strategy.Salt, strategy.Iv, "The Wrong Password");

            try
            {
                strategy.Decrypt(ciphertext);
                Assert.Fail();
            }
            catch (CryptographicException ce)
            {
                // this is expected
            }
        }
        public void test_decrypt_doesNotDecryptTheCiphertext_WhenTheIncorrectIvIsUsed()
        {
            strategy = new AesEncryptionStrategy(128, 1000, "password");
            string ciphertext = strategy.Encrypt("Hello world");

            strategy = new AesEncryptionStrategy(strategy.KeySize, strategy.IterationCount, strategy.Salt, "1DED89E4FB15F61DC6433E3BADA4A891", "password");

            try
            {
                strategy.Decrypt(ciphertext);
                Assert.Fail();
            }
            catch (CryptographicException ce)
            {
                // this is expected
            }
        }
        public void TestAesEncryptionStrategy2(byte[] privateKey)
        {
            var strategy = new AesEncryptionStrategy(privateKey);

            var encryptRequest  = Encoding.UTF8.GetBytes("Hello World!");
            var encryptResponse = strategy.Encrypt(encryptRequest);

            encryptResponse.Length.Should().Be(32);

            // test using a new instance of the strategy
            strategy = new AesEncryptionStrategy(privateKey);

            var decryptRequest  = encryptResponse;
            var decryptResponse = strategy.Decrypt(decryptRequest);

            decryptResponse.SequenceEqual(encryptRequest).Should().BeTrue();
        }