المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Encrypting XML **elements**



C# Programming
01-07-2013, 05:16 AM
I have a requirement to serialize out an object to XML where certain elements are encrypted for privacy / security reasons.

I've seen some samples on the internet for doing this, but they are all hardcoded. I.e. after the object is serialized you "patch" the elements you want to encrypt with the encrypted version. Not liking the hardcoded aspect of this solution. Also having to hardcode the decryption parts.

I've seen another solution where you use the XmlIgnore attribute on the plain text property and then add an "encrypted" version of the property and use the XML attributes to rename it. Not liking that you need 2 versions of every "secure" property.

Are there any other options? Ideally I would like something "in the spirit of .NET" along the lines of:

public class SomeObject
{
[XMLEncrypt]
public string SomeProp { get; set; }

public int SomeIntProp { get; set; }
}

where its something ".NETy" like a custom attribute or something like that. Unfortunately, I don't really see any way to hook into the XML serializer without completely re-writing it.

I thought about deriving a class from XMLSerializer and providing my own Serialize function that calls the base class and then afterwards goes through the object graph via reflection and patches the elements marked with the XMLEncrypt attribute, but I'm not really liking that idea that much either.

Any other suggestions?