Ahmed Shamel
Ahmed Shamel

Reputation: 1962

Add password to Sqlite file in Xamarin forms

I have a Xamarin form application that creates a Sqlite database.

Microsoft.EntityFrameworkCore.Sqlite is used to create the database. I want to add a password to the file. I searched the internet, but unfortunately I can't find any obvious way. On StackOverflow, there are some questions that are similar to my question, however, the platform is not Xamarin Forms.

Here are the questions:

This is my code for creating the database:

public class DoctorDatabaseContext : DbContext
{
        private readonly string DatabasePath;

        public virtual DbSet<OperationsNames> OperationsNames { get; set; }
        public virtual DbSet<CommonChiefComplaint> CommonChiefComplaint { get; set; }
        public virtual DbSet<CommonDiagnosis> CommonDiagnosis { get; set; }
        public virtual DbSet<CommonLabTests> CommonLabTests { get; set; }

        public DoctorDatabaseContext(string DatabasePath)
        {
            FixedDatabasePath.Path = this.DatabasePath = DatabasePath;

            Database.EnsureCreated();    
        }    

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite($"Filename={DatabasePath}");    
        }
    }

Upvotes: 12

Views: 2416

Answers (3)

Dimitar Mazhlekov
Dimitar Mazhlekov

Reputation: 996

Not exactly an answer to your question but a suggestion. You can use a library called Akavache to store data.

There are four storage locations: - BlobCache.LocalMachine - Device storage - BlobCache.UserAccount - User settings. Some systems backup this data to the cloud. - BlobCache.Secure - For saving sensitive data - like credentials.(encrypted) - BlobCache.InMemory - A database, kept in memory. The data is stored for the lifetime of the app.

Hope that helps

Upvotes: 0

bricelam
bricelam

Reputation: 30375

I wrote a post on Encryption in Microsoft.Data.Sqlite. You can leverage it in EF Core by passing in an open connection to UseSqlite.

Instead of Microsoft.EntityFrameworkCore.Sqlite, use these packages:

  • Microsoft.EntityFrameworkCore.Sqlite.Core
  • SQLitePCLRaw.bundle_sqlcipher

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
    // TODO: Dispose with DbContext
    _connection = new SqliteConnection(_connectionString);
    _connection.Open();

    // TODO: Avoid SQL injection (see post)
    var command = _connection.CreateCommand();
    command.CommandText = "PRAGMA key = '" + _password + "'";
    command.ExecuteNonQuery();

    options.UseSqlite(_connection);
}

Upvotes: 8

Barr J
Barr J

Reputation: 10927

There is no way to add password protection for sqlite db out of the box, there is a whole explanation here regarding the reason in more details.

however, there is a nice solution shown on this thread using SQL Chiper to encrypt you DB per page, if you are willing to invest the money.

If you want to encrypt and decrypt your entire db file on demand use PCLCrypto.

But as you noticed, the only way for you to achieve this kind of protection is via encryption of your db file, you won't be able to set password.

Upvotes: 0

Related Questions