简体   繁体   English

模型中双向加密属性的Getter / setter

[英]Getter/setter for two way encrypted property in a model

I have a need to encrypt certain fields in the database, so, I need to encrypt the data going into the database, and then decrypt it when displaying it. 我需要对数据库中的某些字段进行加密,因此,我需要对进入数据库的数据进行加密,然后在显示时将其解密。 I've already got my encryption methods and decryption methods set up and I had it working like this in, for example, an action: 我已经设置好了加密方法和解密方法,并在一个动作中使它像这样工作:

model.EncryptedProperty = Encrypt(viewModel.Property);
viewModel.Property = Decrypt(EncryptedProperty);

That's fine, but my problem with it is that other developers will need to remember to encrypt/decrypt a property any time they use the property. 很好,但是我的问题是其他开发人员在使用属性时,都需要记住对属性进行加密/解密。 This can be a problem for someone new on the project as it requires them to know about this property being encrypted before hand. 对于项目上的新手来说,这可能是个问题,因为它要求他们事先知道此属性已被加密。 I sought to improve the encryption by encrypting/decrypting on the model like so: 我试图通过对模型进行加密/解密来改进加密,如下所示:

private string _property;
public string Property
{
    get { return DecryptString(_property); }
    set { _property = EncryptString(value); }
}

However, this doesn't seem to work, when I view this property in the view, it looks as if though it has encrypted the encrypted data in the database (I have tested this by using DecryptString(DecriptString(_property)) which returns the true value. 但是,这似乎不起作用,当我在视图中查看此属性时,似乎好像已经对数据库中的加密数据进行了加密(我已经使用DecryptString(DecriptString(_property))进行了测试),该数据库返回了真实价值。

What is the solution here? 这里有什么解决方案? Is there a more elegant way to approach this? 有没有更优雅的方式来解决这个问题?

In that scenario, I might do something like: 在这种情况下,我可能会执行以下操作:

[WhateverYourDataLayerNeeds("Property")]
public string EncryptedProperty {get;set;}

public string DecryptedProperty
{
    get { return DecryptString(EncryptedProperty); }
    set { EncryptedProperty = EncryptString(value); }
}

Then the database layer only talks to the first, and there is no confusion. 然后,数据库层与第一层对话,并且没有混乱。

Marc Gravell's answer is definitely the answer matching the question, but if anyone is coming from an Entity Framework code first approach, this is what I ended up doing to obfuscate the encrypted property from any other developers: 马克·格雷韦尔(Marc Gravell)的答案肯定是与问题匹配的答案,但是,如果有人来自Entity Framework代码优先方法,这就是我最终要混淆其他任何开发人员的加密属性的方法:

public class Model 
{
    // Other properties

    private string EncryptedProperty { get; set; }
    [NotMapped]
    public string Property
    {
        get { return Decrypt(EncryptedProperty); }
        return { EncryptedProperty = Encrypt(value); }
    }

    public class ModelConfiguration : EntityTypeConfiguration<Model>
    {
        public ModelConfiguration() 
        {
            Property(p => p.EncryptedProperty);
        }
    }
}

Then in my ApplicationDbContext 然后在我的ApplicationDbContext

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Configurations.Add(new Model.ModelConfiguration);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM