简体   繁体   English

带有 Sql Server 列级加密的实体框架

[英]Entity Framework with Sql Server Column Level Encryption

I have a requirement to encrypt a number of database columns (in Sql Server 2012).我需要加密多个数据库列(在 Sql Server 2012 中)。 It has been decided that we should use column level encryption (implemented in sql server).已经决定我们应该使用列级加密(在 sql server 中实现)。 On the application side i will be building a web api on top of some complex domain models.在应用程序方面,我将在一些复杂的域模型之上构建一个 web api。 I really want to utilize Entity Framework's code first approach, to maintain a clean domain model).我真的很想利用实体框架的代码优先方法来维护一个干净的域模型)。 Does anyone have a workable solution here that does not involve resorting back to stored procedures?有没有人在这里有一个不涉及使用存储过程的可行解决方案? Ideally I would like to somehow manipulate the sql generated by entity framework to wrap certain fields to do the sql encryption / decryption functions.理想情况下,我想以某种方式操纵实体框架生成的 sql 来包装某些字段来执行 sql 加密/解密功能。

Ideally , something like:理想情况下,类似于:

modelBuilder.Entity<MyTable>().ToTable("Table1").Property(p => p.SensativeData).encrypt("keyName",authenticatorFunc);

In SQL Server 2012, column level encryption can be done mainly in two ways ie,在 SQL Server 2012 中,列级加密主要可以通过两种方式完成,即,

  1. Defining Custom Encryption function in Entity framework.在实体框架中定义自定义加密函数 this blog 这个博客
  2. SQL Cell Level Encryption implementation done in entity framework in dbcontext Class (execute open symmetric key code here) using this blog and using stored procedure (which contain decryption code for specified field in tables ) retrieve result sets. SQL 单元级加密实现在 dbcontext 类中的实体框架中完成(在此处执行开放对称密钥代码),使用此博客并使用存储过程(其中包含表中指定字段的解密代码)检索结果集。

In SQL server 2016 there is new feature ie, Always encrypted and has its implementation in entity framework here .在SQL服务器二零一六年,是新的功能,即,始终是加密的,并有其在实体框架实现这里

I know it's a bit old, but if you are using Entity Framework Core, I have developed an Entity Framework Core plugin that handles data encryption of a string field using a custom attribute.我知道它有点旧,但是如果您使用的是 Entity Framework Core,我开发了一个 Entity Framework Core 插件,该插件使用自定义属性处理string字段的数据加密。 Actually there is only the AES encryption provider available, but you can easily implement new encryption providers.实际上只有 AES 加密提供程序可用,但您可以轻松实现新的加密提供程序。 Check it out here: https://github.com/Eastrall/EntityFrameworkCore.DataEncryption在这里查看: https : //github.com/Eastrall/EntityFrameworkCore.DataEncryption

It's compatible with EF Core 2 and 3.它与 EF Core 2 和 3 兼容。

Quick example:快速示例:

public class UserEntity
{
    public int Id { get; set; }

    [Encrypted]
    public string Username { get; set; }

    [Encrypted]
    public string Password { get; set; }

    public int Age { get; set; }
}

public class DatabaseContext : DbContext
{
    // Get key and IV from a Base64String or any other ways.
    // You can generate a key and IV using "AesProvider.GenerateKey()"
    private readonly byte[] _encryptionKey = ...; 
    private readonly byte[] _encryptionIV = ...;
    private readonly IEncryptionProvider _provider;

    public DbSet<UserEntity> Users { get; set; }

    public DatabaseContext(DbContextOptions options)
        : base(options)
    {
        this._provider = new AesProvider(this._encryptionKey, this._encryptionIV);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.UseEncryption(this._provider);
    }
}

Crypteron has a free Entity Framework adapter, CipherDb , that can work with any SQL Server. Crypteron有一个免费的实体框架适配器CipherDb ,它可以与任何 SQL Server 一起使用。 In fact, Crypteron CipherDb works with any Entity Framework compatible database - even MySQL, PostGreSQL and more.事实上,Crypteron CipherDb 适用于任何兼容实体框架的数据库 - 甚至 MySQL、PostGreSQL 等。

You can annotate the data model with [Secure] or name a property to something like Secure_SocialSecurityNumber (the Secure_ is the key part) and CipherDb automatically performs data encryption, tamper protection, secure key storage, secure key distribution, caching, key roll overs, ACLs and more.您可以使用[Secure]注释数据模型或将属性命名为Secure_SocialSecurityNumberSecure_是关键部分),CipherDb 会自动执行数据加密、篡改保护、安全密钥存储、安全密钥分发、缓存、密钥翻转, ACL 等。 You can also use Crypteron to protect streams, files, objects, message queues, noSQL etc.您还可以使用 Crypteron 来保护流、文件、对象、消息队列、noSQL 等。

You can find the sample apps on GitHub at https://github.com/crypteron/crypteron-sample-apps您可以在 GitHub 上找到示例应用程序,网址https://github.com/crypteron/crypteron-sample-apps

Disclaimer: I work there and we do have a free community edition which anyone can use.免责声明:我在那里工作,我们有一个免费的社区版,任何人都可以使用。

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

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