SQLite encryption in Microsoft.EntityFrameWork

1.2k views Asked by At

I’m currently trying to encrypt SQLite database with official SEE extension when using Microsoft entity framework.

I’m able to encrypt database when using ADO.NET. However getting an error “You specified a password in the connection string, but the native SQLite library ‘e_sqlite3’ doesn’t support encryption” when using entity frame work.

Nuget Packages Used:

[Microsoft.EntityFrameWork.Core Microsoft.EntityFrameWork.Core.SQLite SQLite.Encryption.Extension System.Data.SQLite.Core]

Please can you advise how to fix this error with official SEE extension?

CustomDBContext.cs:

private readonly bool _created = false;

public CustomDbContext(DBContextOptions<CustomDbContext> options):base(options){

if(!_created)
{
_created = true;
Database.EnsureCreated();
}
}

public DbSet<SampleEntity> SampleEntities {get; set;}

Program.cs:

static void Main(string[] args)
{
var services = new ServiceCollection();
ConfigureService(services);
using ServiceProvider provider = services.BuildServiceProvider();
provider.GetService<ICustomDBContext>();
}

private static void ConfigureServices(ServiceCollection services)
{
string password = Convert.ToHexString(Encoding.Default.GetBytes("aes256:test");

SQLiteCommand.Execute("PRAGMA activate_extensions='see-7bb07b8d471d642e'", SQLiteExecuteType.NonQuery,@"Data Source=c:\users\test.db");

SQLiteConnectionStringBuilder connectionStringBuilder = new(){
ConnectionString = @"Data Source=c:\users\test.db;Password="+password};

SQLiteConnection conn = new(connectionStringBuilder.ConnectionString);

connection.Open();
connection.ChangePassword(password);
services.AddDbContext<CustomDBContext>(options => options.UseSqlite(connection));
}
1

There are 1 answers

0
parthasarathy tamilselvam On

To use official SQLite extension for encryption, please choose the entity framework until EF6 since the System.Data.SQLite library supports only till EF6 and there is no direct support for EF core.

If we still need to use entity framework core then use we can also consider other options like encryption using SQLCipher that supports EF core.