[英]How do i encrypt and decrypt in model in asp.net mvc 5
I have Model named Users in ASP.NET MVC 5. I have Encrypt() and Decrypt() Extension Methods to Encrypt and Decrypt string respectively. 我在ASP.NET MVC 5中具有名为Users的模型。我具有Encrypt()和Decrypt()扩展方法,分别用于加密和解密字符串。 I want to encrypt and decrypt data while sving and fetching from database. 我想在从数据库中获取数据时进行加密和解密。 So, I used: 因此,我使用了:
private string _mob;
public string mob
{
get
{
return _mob.Decrypt();
}
set
{
_mob = value.Encrypt();
}
}
But I am unable to achieve my goal. 但是我无法实现我的目标。 When I use 当我使用
public string mob
{
get
{
return _mob;
}
set
{
_mob = value.Encrypt();
}
}
I get encryption done but as soon as I add Decrypt() in get{} there is no encryption/decryption done. 我完成了加密,但是一旦在get {}中添加Decrypt(),就不会完成加密/解密。 I see plain text data in database. 我在数据库中看到纯文本数据。
EF will use the property accessors when storing the data in the database, not the backing field, so if you want the encrypted value stored you need to return the encrypted value from the getter. EF在将数据存储在数据库中而不是在后备字段时将使用属性访问器,因此,如果要存储加密的值,则需要从getter返回加密的值。
Since you probably want a property that returns the decrypted value as well you probably want a separate unmapped property for the decrypted text. 由于您可能需要一个返回解密值的属性,因此您可能需要一个单独的未映射属性来处理解密的文本。 You can use the [NotMapped]
attribute so EF doesn't try to save it to the database as well:: 您可以使用[NotMapped]
属性,因此EF也不会尝试将其保存到数据库中:
public string mob {get; set; }
[NotMapped]
public string mobDecrypted
{
get
{
return mob.Decrypt();
}
set
{
mob = value.Encrypt();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.