Encrypt column data using LINQ
I was wondering if this is a simple solution or am I stuck with the following:
When updating the database:
dti.Pass = Crypter.Encrypt (dti.Pass); _db.SubmitChanges ();
When choosing from the database:
Data.DbTableItem dti = _db.Single (a => a.Id == id); dti.Pass = Crypter.Decrypt (dti.Pass);
Value. I am not into writing repetitive code, and this seems to be logical support for LINQ; so I'm wondering if this is the case.
a source to share
You can define a fake property Password
that encapsulates the password logic and the original password field (which appears in the database - PasswordInternal
in my example) should be, for example internal
.
public partial class YourEntity
{
public string Password
{
get
{
return Crypter.Decrypt(this.PasswordInternal)
}
set
{
this.PasswordInternal = Crypter.Encrypt(value)
}
}
}
AFAIK, there are no built-in functions you are looking for.
a source to share
You must use the SQL Server cryptographic functions, ENCRYPTBYKEY and DECRYPTBYKEY . Better yet, use Transparent Database Encryption . Right now, you are encrypting and decrypting the password with some key that knows where. Databases have this nasty habit of moving around and recovering to brand new machines in the event of disaster recovery or as part of various high availability scenarios, and you find that storing encrypted data in the database and encryption key in the system key store (or worse, in the app) left you with a bunch of "completely secure" data that cannot be decrypted because you lost the key.
a source to share