在一个LINQ到SQL实体字段的值加密 [英] Encryption of a field's value in a Linq-to-Sql Entity
问题描述
我需要加密在我的LINQ2SQL实体的一些领域。我也想加密和解密的过程是透明的实体的消费者来说,这意味着一旦实体被加载到内存中的字段是psented作为常规字符串值(解密)$ P $,但相同的字段被加密时,被保存到数据库。
I need to encrypt some fields on my Linq2Sql Entity. I would also like the process of encryption and decryption be transparent to the consumer of the entity, meaning that once the entity is loaded into memory the field is presented as regular value string (decrypted), but the same fields gets encrypted when being persisted to the database.
推荐答案
还有另外一个选择:你可以隐藏的实际使用,例如财产保护
访问修饰符和一个假公开
属性添加到实体部分类,将加密/ decript这个内部
中的getter / setter,所以这将是透明的消费者:
There is another option: you could "hide" the actual property with e.g. protected
access modifier and the add a "fake" public
property to the entity partial class which will encrypt/decript this internal
in getter/setter, so it will be transparent to the consumer:
.dbml文件:
<Column Name="Password"
Member="PasswordInternal"
AccessModifier="Protected"
Type="System.String"
DbType="Varchar(64) NOT NULL"
CanBeNull="false" />
,然后在局部类:
and then in a partial class:
public partial class YourEntity
{
public string Password
{
get
{
return Crypter.Decrypt(this.PasswordInternal)
}
set
{
this.PasswordInternal = Crypter.Encrypt(value)
}
}
}
这篇关于在一个LINQ到SQL实体字段的值加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!