public void Test_That_Default_Encrpytion_Can_Be_Restored_And_That_Algorithms_Are_Applied_Correctly()
        {
            // Arrange
            var serializableObject = new SerializableClass { Field = "Test_That_Default_Encrpytion_Can_Be_Restored_And_That_Algorithms_Are_Applied_Correctly" };

            var streamWithEncryption = new List<byte>();
            var streamWithDefaultEncryption = new List<byte>();
            var streamWithOverriddenEncryption = new List<byte>();
            var streamWithRestoredDefaultEncryption = new List<byte>();

            var dataContractSerializer = new DataContractSerializer(serializableObject.GetType());
            var serializerEncryptor = new DataContractSerializerEncrpytor(dataContractSerializer);

            // Setup a mock stream
            var mockStream = new Mock<Stream>();
            mockStream.SetupAllProperties();
            mockStream.Setup(stream => stream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
                .Callback(
                    (byte[] buffer, int offset, int count) =>
                        streamWithEncryption.AddRange(buffer));
            mockStream.SetupGet(stream => stream.CanWrite).Returns(true);

            // Act
            // Setup default encrpytion output
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            streamWithDefaultEncryption.AddRange(streamWithEncryption);

            // Change and apply encrpytion
            serializerEncryptor.OverrideEncryption(
                new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 },
                new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 },
                new DESCryptoServiceProvider());

            streamWithEncryption.Clear();
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            streamWithOverriddenEncryption.AddRange(streamWithEncryption);

            // Now change the encrpytion back
            serializerEncryptor.RestoreDefaultEncryption();
            streamWithEncryption.Clear();
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            streamWithRestoredDefaultEncryption.AddRange(streamWithEncryption);

            // Assert
            // Our stream with default encrpytion should equal our stream with restored encryption
            Assert.IsTrue(streamWithDefaultEncryption.SequenceEqual(streamWithRestoredDefaultEncryption));

            // Both default and restored encryption schemes should be differed from the overriden one.
            Assert.IsFalse(streamWithDefaultEncryption.SequenceEqual(streamWithOverriddenEncryption));
            Assert.IsFalse(streamWithRestoredDefaultEncryption.SequenceEqual(streamWithOverriddenEncryption));
        }
        public void Verify_That_Overriden_Encryption_Is_Applied_When_Key_And_Iv_Are_Overriden()
        {
            // Arrange
            var serializableObject = new SerializableClass { Field = "Verify_That_Overriden_Encryption_Is_Applied_When_Key_And_Iv_Are_Overriden" };

            var streamWithEncryption = new List<byte>();
            var streamWithDefaultEncryption = new List<byte>();
            var streamWithOverriddenEncryption = new List<byte>();

            var dataContractSerializer = new DataContractSerializer(serializableObject.GetType());
            var serializerEncryptor = new DataContractSerializerEncrpytor(dataContractSerializer);

            // Setup a mock stream
            var mockStream = new Mock<Stream>();
            mockStream.SetupAllProperties();
            mockStream.Setup(stream => stream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
                .Callback(
                    (byte[] buffer, int offset, int count) =>
                        streamWithEncryption.AddRange(buffer));
            mockStream.SetupGet(stream => stream.CanWrite).Returns(true);

            // Act
            // Setup default encrpytion output
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            streamWithDefaultEncryption.AddRange(streamWithEncryption);
            streamWithEncryption.Clear();

            // Change and apply encrpytion
            serializerEncryptor.OverrideEncryption(
                new byte[] { 9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2 },
                new byte[] { 9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 });
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            streamWithOverriddenEncryption.AddRange(streamWithEncryption);

            // Assert
            Assert.IsFalse(streamWithDefaultEncryption.SequenceEqual(streamWithOverriddenEncryption));
        }
        public void Test_That_We_Can_Decrpyt_A_Stream_That_Was_Encrypted_With_Overridden_Encryption_Parameters()
        {
            // Arrange
            var serializableObject = new SerializableClass { Field = "Test_That_We_Can_Decrpyt_A_Stream_That_Was_Encrypted_With_Overridden_Encryption_Parameters" };

            var streamWithEncryption = new List<byte>();

            var dataContractSerializer = new DataContractSerializer(serializableObject.GetType());
            var serializerEncryptor = new DataContractSerializerEncrpytor(dataContractSerializer);

            // Setup a mock stream for writing
            var mockStreamForWriting = new Mock<Stream>();
            mockStreamForWriting.SetupAllProperties();
            mockStreamForWriting.SetupGet(stream => stream.CanWrite).Returns(true);
            mockStreamForWriting.Setup(stream => stream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
                .Callback(
                    (byte[] buffer, int offset, int count) =>
                        streamWithEncryption.AddRange(buffer));

            serializerEncryptor.OverrideEncryption(
                new byte[] { 9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2 },
                new byte[] { 9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 });

            // Perform encryption to get encrypted byte array
            serializerEncryptor.WriteObjectEncrypted(mockStreamForWriting.Object, serializableObject);

            // New create a memory stream based on this, so we can pass it back into the decrpytor
            var streamForReading = new MemoryStream(streamWithEncryption.ToArray());

            // Act
            var decryptedObject = (SerializableClass)serializerEncryptor.ReadObjectEncrypted(streamForReading);

            // Assert
            Assert.AreEqual(serializableObject.Field, decryptedObject.Field);
        }
        public void Test_That_Exception_Is_Thrown_When_Null_newEncryptionKey_Is_Passed_To_Encrpytion_Override()
        {
            // Arrange
            var serializableObject = new SerializableClass { Field = "Field" };

            var dataContractSerializer = new DataContractSerializer(serializableObject.GetType());
            var serializerEncryptor = new DataContractSerializerEncrpytor(dataContractSerializer);

            serializerEncryptor.OverrideEncryption(
                null,
                new byte[] { 9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 });
        }