Help me find an elegant solution for custom encryption with the Entity Framework RRS feed

  • Question

  • We have a legacy database that we are trying to use with the entity framework (4.0) for a new project. 

    We have previously invented our own custom encryption for all varchar columns.

    We have 2 methods that deal with this encryption:

    string encrypted= encryptionHelper.Encrypt(string original)

    string original= encryptionHelper.Decrypt(string encrypted)

    So I'm looking for an elegant solution to stop this logic polluting my code (encrypt data before it gets added to the db and decrypt the data as it comes out).

    Ideas so far:

    • Pollute my code with the encryptionHelper
    • Modify all DTO to have properties for retriving the encoded value of a property e.g MyPropertyX and MyPropertyX Encoded (Ugly and involves modifying 500 + properties)
    • Hook into a pre-save event (like SaveChanges) and use reflection to encrypt all strings and do the opposite on Get requests? (is there an similar Get event?) would i have to unencrypt the properties after the save so that the code still has the unencrypted code?
    • Create a custom character set MySpecialCharacterSet that deals with the encryption and somehow get EF to use this? (not sure how or if this is possible?)


    Any other ideas? All have many relevant threads and they are all old and don't find a solution. They just link to a dead page at safnet.com/writing/tech/archives/2009/08/encryption_view.html


    Thanks for your help!




    • Edited by joe147 Wednesday, November 16, 2011 3:12 PM
    Wednesday, November 16, 2011 3:04 PM

All replies

    Are you using this in some kind of Web based solution?
    The easiest thing to do is use DTO(s) with encrypt string data on
    property set and decrypt on the get.
    Of course you would have to map entities to/from DTO(s).
    Wednesday, November 16, 2011 3:26 PM
  • but then my entities would be encrypted and my code would need to encrypt them. I was looking for a more transparent solution like looking into onSave and onQuery in one place.


    Friday, November 18, 2011 11:06 AM
  • Saving Changes Event is raised before a SaveChanges operation is processed: http://msdn.microsoft.com/en-us/library/cc716714.aspx
    I am fish.
    Tuesday, November 22, 2011 7:13 AM