public void EncryptsAndDecryptsGenericDictionaryPropertyWhenTheKeyClassIsDecoratedWithEncryptAttribute() { IEncryptionMechanism encryptionMechanism = new Base64EncryptionMechanism(); var serializer = new XmlSerializer <Container <Dictionary <EncryptedThing, int> > >(x => x.WithEncryptionMechanism(encryptionMechanism)); var instance = new Container <Dictionary <EncryptedThing, int> > { Item = new Dictionary <EncryptedThing, int> { { new EncryptedThing { Foo = 123, Bar = true }, 1 }, { new EncryptedThing { Foo = 789, Bar = false }, 2 }, } }; var xml = serializer.Serialize(instance); var doc = XDocument.Parse(xml); var actualDecryptedItemElementValue = encryptionMechanism.Decrypt(doc.Root.Element("Item").Value, null, _serializationState); const string expectedDecryptedItemElementValue = @"<Item><Key Foo=""123""><Bar>true</Bar></Key><Value>1</Value></Item>" + @"<Item><Key Foo=""789""><Bar>false</Bar></Key><Value>2</Value></Item>"; Assert.That(actualDecryptedItemElementValue, Is.EqualTo(expectedDecryptedItemElementValue)); var roundTrip = serializer.Deserialize(xml); Assert.That(roundTrip.Item.Keys, Is.EquivalentTo(instance.Item.Keys)); var key = new EncryptedThing { Foo = 123, Bar = true }; Assert.That(roundTrip.Item[key], Is.EqualTo(instance.Item[key])); key = new EncryptedThing { Foo = 789, Bar = false }; Assert.That(roundTrip.Item[key], Is.EqualTo(instance.Item[key])); }
public void CanEncryptAndDecryptEntireRootObjectViaEncryptAttribute() { IEncryptionMechanism encryptionMechanism = new Base64EncryptionMechanism(); var serializer = new XmlSerializer <EncryptedThing>(x => x.WithEncryptionMechanism(encryptionMechanism)); var instance = new EncryptedThing { Foo = 123, Bar = true }; var xml = serializer.Serialize(instance); var doc = XDocument.Parse(xml); Assert.That(encryptionMechanism.Decrypt(doc.Root.Value, null, _serializationState), Is.EqualTo("<Bar>true</Bar>")); Assert.That(encryptionMechanism.Decrypt(doc.Root.Attribute("Foo").Value, null, _serializationState), Is.EqualTo("123")); var roundTrip = serializer.Deserialize(xml); Assert.That(roundTrip.Foo, Is.EqualTo(instance.Foo)); Assert.That(roundTrip.Bar, Is.EqualTo(instance.Bar)); }