Liss
Liss

Reputation: 71

How to encrypt xml elements?

I have auto generated a class (vb.net) from an xsd schema (with xsd.exe). I populate the objects properties with data and serialize the object to xml and store the xml in a string. In my xml I have to encrypt some of the elements. What is the best way to do this?

Can I encrypt my objects properties before I serialize the object to xml? In my documentation to the xsd schema it says that all the encrypted values (elements) has to be string, but when I auto generate the class from the xsd schem the Birth number is set to date and not string…? And birth number is part of the elements I have to encrypt. I want to use PKI to encrypt the symmetric key I want to use to encrypt the xml elements.

Can somebody help me with this? Thanks!

Upvotes: 1

Views: 697

Answers (1)

Rich
Rich

Reputation: 15448

One straightforward solution might be to expose two versions of each property, encrypted and unencrypted, and mark all the unencrypted versions with [XmlIgnore].

If you're unable to edit the auto-generated classes, and you want those properties to be encrypted, then you could add un-encrypted wrapper properties in the partial class, e.g.

== in the auto-generated file:

class MyClass {
  String SensitiveProperty { get; set; }
}

== in the not-auto-generated partial file:

partial class MyClass {
  [XmlIgnore]
  String SensitivePropertyDecrypted {
    get {
      return CryptoHelper.Decrypt(SensitiveProperty);
    }
    set {
      SensitiveProperty = CryptoHelper.Encrypt(value);
    }
  }
}

... where "CryptoHelper" is a class you've written to support the encryption scheme you're hoping to achieve (i.e. using the symmetric key you've passed using PKI).

Upvotes: 1

Related Questions