EntityFrameworkCore.DataEncryption

repository·main·Indexed 43 days ago

https://github.com/softfluent/entityframeworkcore.dataencryption

An extension for Microsoft Entity Framework Core that enables encryption of specific database columns using the [Encrypted] attribute, Fluent API .IsEncrypted() method, or custom IEncryptionProvider implementations. It supports string and byte[] data types and includes a built-in AesProvider for 128-bit, 192-bit, or 256-bit keys.

Tokens
1.5K
Snippets
4
Records
6
Agent score
6%

What's inside EntityFrameworkCore.DataEncryption

  1. Implement a custom IEncryptionProvider

    main

    To create a custom encryption provider, implement the IEncryptionProvider interface. You must provide implementations for Encrypt(byte[] input) and Decrypt(byte[] input).

    Once implemented, instantiate your provider in your DbContext and pass it to modelBuilder.UseEncryption(_provider).

    public class MyCustomEncryptionProvider : IEncryptionProvider
    {
    	public byte[] Encrypt(byte[] input)
    	{
    		// Encrypt the given input and return the encrypted data as a byte[].
    	}
    	
    	public byte[] Decrypt(byte[] input)
    	{
    		// Decrypt the given input and return the decrypted data as a byte[].
    	}
    }
    
    public class DatabaseContext : DbContext
    {
    	private readonly IEncryptionProvider _provider;
    
    	public DatabaseContext(DbContextOptions options)
    		: base(options)
    	{
    		_provider = new MyCustomEncryptionProvider();
    	}
    
    	protected override void OnModelCreating(ModelBuilder modelBuilder)
    	{
    		modelBuilder.UseEncryption(_provider);
    	}
    }
  2. Encrypt fields using the [Encrypted] attribute

    main

    You can mark string or byte[] properties for encryption by decorating them with the [Encrypted] attribute. To enable this, you must call modelBuilder.UseEncryption(_provider) in your DbContext.OnModelCreating method.

    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
    {
    	private readonly byte[] _encryptionKey = ...; 
    	private readonly byte[] _encryptionIV = ...;
    	private readonly IEncryptionProvider _provider;
    
    	public DbSet<UserEntity> Users { get; set; }
    	
    	public DatabaseContext(DbContextOptions options)
    		: base(options)
    	{
    		_provider = new AesProvider(this._encryptionKey, this._encryptionIV);
    	}
    	
    	protected override void OnModelCreating(ModelBuilder modelBuilder)
    	{
    		modelBuilder.UseEncryption(_provider);
    	}
    }
  3. Encrypt fields using Fluent API configuration

    main

    Instead of attributes, you can use the .IsEncrypted() method within your model configuration.

    Important: Entity configuration (e.g., modelBuilder.Entity<T>()) MUST be called before modelBuilder.UseEncryption(_provider).

    public class UserEntity
    {
    	public int Id { get; set; }
    	public string Username { get; set; }
    	public string Password { get; set; }
    	public int Age { get; set; }
    }
    
    public class DatabaseContext : DbContext
    {
    	private readonly byte[] _encryptionKey = ...; 
    	private readonly byte[] _encryptionIV = ...;
    	private readonly IEncryptionProvider _provider;
    
    	public DbSet<UserEntity> Users { get; set; }
    	
    	public DatabaseContext(DbContextOptions options)
    		: base(options)
    	{
    		_provider = new AesProvider(this._encryptionKey, this._encryptionIV);
    	}
    	
    	protected override void OnModelCreating(ModelBuilder modelBuilder)
    	{
    		// Entities builder *MUST* be called before UseEncryption().
    		var userEntityBuilder = modelBuilder.Entity<UserEntity>();
    		
    		userEntityBuilder.Property(x => x.Username).IsRequired().IsEncrypted();
    		userEntityBuilder.Property(x => x.Password).IsRequired().IsEncrypted();
    
    		modelBuilder.UseEncryption(_provider);
    	}
    }