Protect an SQLite database in an UWP app

半城伤御伤魂 提交于 2020-03-26 07:19:39

问题


It is an UWPapplication using a SQLite database. Below, the dependencies for this application:

{
    "dependencies": {
    "Microsoft.EntityFrameworkCore.Sqlite": "1.0.1",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2",
    "Microsoft.Xaml.Behaviors.Uwp.Managed": "1.1.0",
    "Newtonsoft.Json": "8.0.3",
    "Template10": "1.1.*"
    },
    // ...
}

The requirement is: "[...]to have a password to access the database either from the application or any other application that can open a SQLite database".

Entity Framework Core doesn't seem to support this scenario. Any suggestion?


回答1:


See my Encryption in Microsoft.Data.Sqlite post for tips on using SQLCipher and friends with Microsoft.Data.Sqlite.

The easiest way to use it with EF Core is probably to use an open connection with your DbContext.

class MyContext : DbContext
{
    SqliteConnection _connection;

    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        _connection = new SqliteConnection(_connectionString);
        _connection.Open();

        var command = _connection.CreateCommand();
        command.CommandText = "PRAGMA key = 'password';";
        command.ExecuteNonQuery();

        options.UseSqlite(_connection);
    }

    protected override void Dispose()
    {
        _connection?.Dispose();
    }
}



回答2:


Take a look at SQLCipher (https://github.com/sqlcipher/sqlcipher). It provides seamless full DB encryption with little overhead. It is a pain to build a VSIX for use with Visual Studio. If you do not want to build it yourself, yo can get a license from https://www.zetetic.net/sqlcipher/.



来源:https://stackoverflow.com/questions/39764246/protect-an-sqlite-database-in-an-uwp-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!