EntityFrameworkCore.DataEncryption 3.0.1

Additional Details

Retired from open-source. This package will not be updated anymore. Please use another NuGet package.

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package EntityFrameworkCore.DataEncryption --version 3.0.1
NuGet\Install-Package EntityFrameworkCore.DataEncryption -Version 3.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EntityFrameworkCore.DataEncryption" Version="3.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityFrameworkCore.DataEncryption --version 3.0.1
#r "nuget: EntityFrameworkCore.DataEncryption, 3.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EntityFrameworkCore.DataEncryption as a Cake Addin
#addin nuget:?package=EntityFrameworkCore.DataEncryption&version=3.0.1

// Install EntityFrameworkCore.DataEncryption as a Cake Tool
#tool nuget:?package=EntityFrameworkCore.DataEncryption&version=3.0.1

EntityFrameworkCore.DataEncryption

Build Status codecov Nuget

EntityFrameworkCore.DataEncryption is a Microsoft Entity Framework Core extension to add support of encrypted fields using built-in or custom encryption providers.

Disclaimer

This library has been developed initialy for a personal project of mine. It provides a simple way to encrypt column data.

I do not take responsability if you use this in a production environment and loose your encryption key.

How to install

Install the package from NuGet or from the Package Manager Console :

PM> Install-Package EntityFrameworkCore.DataEncryption

How to use

To use EntityFrameworkCore.DataEncryption, you will need to decorate your string properties of your entities with the [Encrypted] attribute and enable the encryption on the ModelBuilder.

To enable the encryption correctly, you will need to use an encryption provider, there is a list of the available providers:

Name Class Extra
AES AesProvider Can use a 128bits, 192bits or 256bits key

Example with AesProvider

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);
	}
}

The code bellow creates a new AesEncryption provider and gives it to the current model. It will encrypt every string fields of your model that has the [Encrypted] attribute when saving changes to database. As for the decrypt process, it will be done when reading the DbSet<T> of your DbContext.

Create an encryption provider

⚠️ This section is outdated and doesn't work for V3.0.0 and will be updated soon.

EntityFrameworkCore.DataEncryption gives the possibility to create your own encryption providers. To do so, create a new class and make it inherit from IEncryptionProvider. You will need to implement the Encrypt(string) and Decrypt(string) methods.

public class MyCustomEncryptionProvider : IEncryptionProvider
{
	public string Encrypt(string dataToEncrypt)
	{
		// Encrypt data and return as Base64 string
	}
	
	public string Decrypt(string dataToDecrypt)
	{
		// Decrypt a Base64 string to plain string
	}
}

To use it, simply create a new MyCustomEncryptionProvider in your DbContext and pass it to the UseEncryption method:

public class DatabaseContext : DbContext
{
	private readonly IEncryptionProvider _provider;

	public DatabaseContext(DbContextOptions options)
		: base(options)
	{
		this._provider = new MyCustomEncryptionProvider();
	}

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

Important notes

AES Provider structure

The following section describes how encrypted fields using the built-in AES provider encrypts data. There is two available modes :

  • Fixed IV
  • Dynamic IV
Fixed IV

A fixed IV is generated at setup and is used for every encrypted fields on the database. This might be a security issue depending on your context.

Dynamic IV

For each encrypted field, the provider generates a new IV with a length of 16 bytes. These 16 bytes are written at the begining of the CryptoStream followed by the actual input to encrypt.

Similarly, for reading, the provider reads the first 16 bytes from the input data converted as a byte[] to retrieve the initialization vector and then read the encrypted content.

For more information, checkout the AesProvider class.

⚠️ When using Dynamic IV, you cannot use the Entity Framework LINQ extensions because the provider will generate a new IV per value, which will create unexpected behaviors.

Thanks

I would like to thank all the people that supports and contributes to the project and helped to improve the library. 😄

Credits

Package Icon : from Icons8

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on EntityFrameworkCore.DataEncryption:

Package Downloads
FamilyHubs.SharedKernel

Package Description

Perigee.Framework.EntityFramework

Package Description

TBC.VNext.Framework.Domain

Package Description

GuiLi.Abp.Crypto.EntityFrameworkCore

Package Description

Lincee.Framework.VNext.EntityFrameworkCore

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on EntityFrameworkCore.DataEncryption:

Repository Stars
jeangatto/ASP.NET-Core-Clean-Architecture-CQRS-Event-Sourcing
ASP.NET Core, C#, CQRS Event Sourcing, REST API, DDD, SOLID Principles and Clean Architecture
Version Downloads Last updated

Add support for .NET 5 and .NET 6