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
<PackageReference Include="EntityFrameworkCore.Crypto.DataEncryption" Version="9.0.0" />
<PackageVersion Include="EntityFrameworkCore.Crypto.DataEncryption" Version="9.0.0" />
<PackageReference Include="EntityFrameworkCore.Crypto.DataEncryption" />
paket add EntityFrameworkCore.Crypto.DataEncryption --version 9.0.0
#r "nuget: EntityFrameworkCore.Crypto.DataEncryption, 9.0.0"
#:package EntityFrameworkCore.Crypto.DataEncryption@9.0.0
#addin nuget:?package=EntityFrameworkCore.Crypto.DataEncryption&version=9.0.0
#tool nuget:?package=EntityFrameworkCore.Crypto.DataEncryption&version=9.0.0
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 | Versions 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. |
-
net7.0
- Microsoft.EntityFrameworkCore (>= 7.0.0)
-
net8.0
- Microsoft.EntityFrameworkCore (>= 8.0.0)
-
net9.0
- Microsoft.EntityFrameworkCore (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.