Column programming RRS feed

  • Question

  • Hi

    I have a column in a table which I would like to encrypt, I have lots of code which access this column so I would like to use the same codem but transperently ( when reading - descrypt ) and save ( encrypt )

    Any help would be really usefull

    Tuesday, March 3, 2009 5:06 PM

All replies

  • If the column is not used for selections/group-by's and other server-side operations one way you could do this would be to change the column's access to private (perhaps renaming it e.g. SalaryEncrypted) and then create a new property in the partial class called Salary with a get and set that refers to the encrypted field wrapped up with the encryption/decryption methods, e.g.

    public Decimal Salary { get { return MyEncryption.Decrypt(this.SalaryEncrypted); } set { this.SalaryEncrypted = MyEncryption.Encrypt(value); } }

    Tuesday, March 3, 2009 7:23 PM
  • Hi thanks for the reply, but ideally I would like to leave the existing code, which does selects etc alone. Is there some means that when I access the column then it transperently gets encrypted/decrypted.

    <Table(Name:="dbo.CompanySetting"), _

    DataContract()> _

    Partial Public Class CompanySetting

    <Association(Name:="Company_CompanySetting", Storage:="_Company", ThisKey:="CompanyId", IsForeignKey:=true)> _

    Public Property Company() As Company


    Return Me._Company.Entity

    End Get


    Dim previousValue As Company = Me._Company.Entity

    If ((Object.Equals(previousValue, value) = false) _

    OrElse (Me._Company.HasLoadedOrAssignedValue = false)) Then


    If ((previousValue Is Nothing) _

    = false) Then

    Me._Company.Entity = Nothing

    previousValue.CompanySetting = Nothing

    End If

    Me._Company.Entity = value

    If ((value Is Nothing) _

    = false) Then

    value.CompanySetting = Me

    Me._CompanyId = value.CompanyId


    Me._CompanyId = CType(Nothing, Integer)

    End If


    End If

    End Set

    End Property

    Wednesday, March 4, 2009 4:18 PM
  • Are you selecting based on your encrypted column?

    Friday, March 6, 2009 4:29 PM
  • Hi

    I am doing stuff like

    dim Customers = from c in db.Customers where c.Company = 'Fred'

    When I do this I would like it to replace 'Fred' with an encrypted version
    Friday, March 6, 2009 4:43 PM
  • There is nothing built-in to handle this so you will need to just decrypt first and then use that, e.g.

    string company = Decrypt('Fred');
    var customers = from c in db.Customers where c.Company = company;

    Sunday, March 8, 2009 5:51 PM