EntityFrameworkCore.Crypto.DataEncryption 9.0.0

dotnet add package EntityFrameworkCore.Crypto.DataEncryption --version 9.0.0
                    
NuGet\Install-Package EntityFrameworkCore.Crypto.DataEncryption -Version 9.0.0
                    
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.Crypto.DataEncryption" Version="9.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EntityFrameworkCore.Crypto.DataEncryption" Version="9.0.0" />
                    
Directory.Packages.props
<PackageReference Include="EntityFrameworkCore.Crypto.DataEncryption" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EntityFrameworkCore.Crypto.DataEncryption --version 9.0.0
                    
#r "nuget: EntityFrameworkCore.Crypto.DataEncryption, 9.0.0"
                    
#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.
#:package EntityFrameworkCore.Crypto.DataEncryption@9.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EntityFrameworkCore.Crypto.DataEncryption&version=9.0.0
                    
Install as a Cake Addin
#tool nuget:?package=EntityFrameworkCore.Crypto.DataEncryption&version=9.0.0
                    
Install as a Cake Tool

Disclaimer

<h4 align="center">:warning: This package/product is not affiliated with Microsoft. ⚠️</h4><br>

the package has been developed for a project of Royce Lark pvt,ltd which suits our use case. It includes a way to encrypt the column data via context.

Royce Lark do not take responsability if you use/deploy this in a production environment and loose your encryption key or corrupt your data.

<h4 align="center">:warning: if you want help/support you must buy a support licence. to buy a licence contact web.html123@gmail.com:warning:</h4><br>

How to install

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

PM> Install-Package MicrosoftEntityFrameworkCore.Crypto.DataEncryption

Supported types

Type Default storage type
string Base64 string
byte[] BINARY

How to use

EntityFrameworkCore.Crypto.DataEncryption supports 2 differents initialization methods:

  • Attribute
  • Fluent configuration

Depending on the initialization method you will use, you will need to decorate your string or byte[] properties of your entities with the [CryptoEncrypted] attribute or use the fluent IsEncrypted() method in your model configuration process. To use an encryption provider on your EF Core model, and enable the encryption on the CryptoModelBuilder.

Example with AesProvider and attribute

public class UserEntity
{
	public int Id { get; set; }
	
	[CryptoEncrypted]
	public string Username { get; set; }
	
	[CryptoEncrypted]
	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 "CryptoAesProvider.GenerateKey()"
	private readonly byte[] _encryptionKey = ...; 
	private readonly byte[] _encryptionIV = ...;
	private readonly IEncryptionCryptoProvider _provider;

	public DbSet<UserEntity> Users { get; set; }
	
	public DatabaseContext(DbContextOptions options)
		: base(options)
	{
		_provider = new CryptoAesProvider(this._encryptionKey, this._encryptionIV);
	}
	
	protected override void OnModelCreating(CryptoModelBuilder modelBuilder)
	{
		modelBuilder.UseEncryption(_provider);
	}
}

Example with CryptoAesProvider and fluent configuration

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
{
	// Get key and IV from a Base64String or any other ways.
	// You can generate a key and IV using "CryptoAesProvider.GenerateKey()"
	private readonly byte[] _encryptionKey = ...; 
	private readonly byte[] _encryptionIV = ...;
	private readonly IEncryptionCryptoProvider _provider;

	public DbSet<UserEntity> Users { get; set; }
	
	public DatabaseContext(DbContextOptions options)
		: base(options)
	{
		_provider = new CryptoAesProvider(this._encryptionKey, this._encryptionIV);
	}
	
	protected override void OnModelCreating(CryptoModelBuilder 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);
	}
}

Create an encryption provider

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

public class MyCustomEncryptionProvider : IEncryptionCryptoProvider
{
	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[].
	}
}

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

public class DatabaseContext : DbContext
{
	private readonly IEncryptionCryptoProvider _provider;

	public DatabaseContext(DbContextOptions options)
		: base(options)
	{
		_provider = new MyCustomEncryptionryptoCProvider();
	}

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

Thanks

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

<h4 align="center"> If You Would Like To Buy Me A Coffee... You Can Donate Via Paypal: https://www.paypal.me/Mohang2 </h4><br>

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.0.0 141 6/23/2025
8.0.3 53,574 5/29/2024
8.0.2 158 5/27/2024
8.0.1 135 5/27/2024
8.0.0 184 5/24/2024 8.0.0 is deprecated because it is no longer maintained.