public void Read_EmptyStream_EmptyStreamEncrypted()
        {
            var cryptoStream = new SymmetricEncryptStream(
                new MemoryStream(),
                new SymmetricEncryptionSettings
            {
                Key = GenerateKey(256)
            });

            var result = cryptoStream.ReadAll();

            result.Should().HaveCount(32);
        }
        public void Read_WithoutSpecifyingIV_IVIsGeneratedAndPrepended()
        {
            var cryptoStream = new SymmetricEncryptStream(
                new MemoryStream(ClearTextMessage),
                new SymmetricEncryptionSettings
            {
                Key = GenerateKey(256)
            });

            var result = cryptoStream.ReadAll();

            result.Should().NotBeNull();
            result !.Length.Should().Be(AesDefaultBlockSizeInBytes + AesDefaultInitializationVectorSizeInBytes);
        }
        public void Read_UsingDefaultAesAlgorithmWithDefaultSettings_MessageIsSuccessfullyEncrypted()
        {
            var cryptoStream = new SymmetricEncryptStream(
                new MemoryStream(ClearTextMessage),
                new SymmetricEncryptionSettings
            {
                Key = GenerateKey(256)
            });

            var result = cryptoStream.ReadAll();

            result.Should().NotBeNull();
            result !.Length.Should().Be(AesDefaultBlockSizeInBytes + AesDefaultInitializationVectorSizeInBytes);
            result.Should().NotBeEquivalentTo(ClearTextMessage);
        }
        public void Read_SpecifyingIV_IVIsNotPrepended()
        {
            var iv = GenerateKey(128);

            var cryptoStream = new SymmetricEncryptStream(
                new MemoryStream(ClearTextMessage),
                new SymmetricEncryptionSettings
            {
                Key = GenerateKey(256),
                InitializationVector = iv
            });

            var result = cryptoStream.ReadAll();

            result.Should().NotBeNull();
            result !.Length.Should().Be(AesDefaultBlockSizeInBytes);

            result.Take(iv.Length).Should().NotBeEquivalentTo(iv);
        }
        public void Read_UsingRijndaelWithCustomSettings_MessageIsSuccessfullyEncrypted()
        {
            var cryptoStream = new SymmetricEncryptStream(
                new MemoryStream(ClearTextMessage),
                new SymmetricEncryptionSettings
            {
                AlgorithmName = "Rijndael",
                BlockSize     = 128,
                FeedbackSize  = 64,
                Key           = GenerateKey(128),
                CipherMode    = CipherMode.ECB,
                PaddingMode   = PaddingMode.ISO10126
            });

            var result = cryptoStream.ReadAll();

            result.Should().NotBeNull();
            result !.Length.Should().Be((128 * 2) / 8);
            result.Should().NotBeEquivalentTo(ClearTextMessage);
        }
        public void Read_TwiceWithoutSpecifyingIV_ResultIsDifferent()
        {
            var cryptoStream1 = new SymmetricEncryptStream(
                new MemoryStream(ClearTextMessage),
                new SymmetricEncryptionSettings
            {
                Key = GenerateKey(256)
            });
            var cryptoStream2 = new SymmetricEncryptStream(
                new MemoryStream(ClearTextMessage),
                new SymmetricEncryptionSettings
            {
                Key = GenerateKey(256)
            });

            var result1 = cryptoStream1.ReadAll();
            var result2 = cryptoStream2.ReadAll();

            result2.Should().NotBeEquivalentTo(result1);
        }
        public void Read_TwiceWithSameIV_ResultIsEqual()
        {
            var cryptoStream1 = new SymmetricEncryptStream(
                new MemoryStream(ClearTextMessage),
                new SymmetricEncryptionSettings
            {
                Key = GenerateKey(256),
                InitializationVector = GenerateKey(128)
            });
            var cryptoStream2 = new SymmetricEncryptStream(
                new MemoryStream(ClearTextMessage),
                new SymmetricEncryptionSettings
            {
                Key = GenerateKey(256),
                InitializationVector = GenerateKey(128)
            });

            var result1 = cryptoStream1.ReadAll();
            var result2 = cryptoStream2.ReadAll();

            result2.Should().BeEquivalentTo(result1);
        }
        public void Read_TwiceWithoutSpecifyingIV_GeneratedIVIsDifferent()
        {
            var cryptoStream1 = new SymmetricEncryptStream(
                new MemoryStream(ClearTextMessage),
                new SymmetricEncryptionSettings
            {
                Key = GenerateKey(256)
            });
            var cryptoStream2 = new SymmetricEncryptStream(
                new MemoryStream(ClearTextMessage),
                new SymmetricEncryptionSettings
            {
                Key = GenerateKey(256)
            });

            var result1 = cryptoStream1.ReadAll();
            var result2 = cryptoStream2.ReadAll();

            var iv1 = result1 !.Take(AesDefaultInitializationVectorSizeInBytes);
            var iv2 = result2 !.Take(AesDefaultInitializationVectorSizeInBytes);

            iv2.Should().NotBeEquivalentTo(iv1);
        }