This library provides a simple way to encrypt and decrypt column-level data in Entity Framework Core using an encryption provider.
I appreciate every star ⭐ that my projects receive, and your support means a lot to me! If you find my projects useful or enjoyable, please consider giving them a star.
- Automatic encryption and decryption of string properties marked with
@EncryptColumn. - Uses AES encryption for secure data storage.
- Easy integration with
DbContextusingModelBuilderextensions.
To use this package, add the required dependencies to your .NET project:
You can install this template using NuGet:
// Install EF Core and required packages
dotnet add package EntityFrameworkCore.ColumnLevelEncryptionModify your DbContext to use the encryption provider:
using Microsoft.EntityFrameworkCore;
using EntityFrameworkCore.ColumnLevelEncryption.Util;
public class BetContext : DbContext
{
private readonly GenerateEncryptionProvider _provider;
public BetContext(DbContextOptions<BetContext> options) : base(options)
{
_provider = new GenerateEncryptionProvider("WOXcoTgvWTh+PXaYCAfiEQ==");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UseEncryption(_provider);
base.OnModelCreating(modelBuilder);
}
}Add the @EncryptColumn attribute to properties that should be encrypted:
using EntityFrameworkCore.ColumnLevelEncryption.Attribute;
public class User
{
public int Id { get; set; }
[EncryptColumn]
public string U_PASSWORD { get; set; }
}- When Entity Framework Core interacts with the database, properties marked with
@EncryptColumnare automatically encrypted before saving and decrypted when retrieved. - The
ModelBuilderExtensionsclass scans all entity types and applies the encryption logic to properties with the@EncryptColumnattribute.
- The encryption key should be securely stored, not hardcoded in the application.
- Consider using a key management service (e.g., Azure Key Vault, AWS KMS) to securely handle encryption keys.
This project is licensed under the MIT License.