Password Protect a SQLite Db. Is It Possible

Password Protect a SQLite DB. Is it possible?

You can password protect a SQLite3 DB. Before doing any operations, set the password as follows.

SQLiteConnection conn = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
conn.SetPassword("password");
conn.Open();

then next time you can access it like

conn = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;Password=password;");
conn.Open();

This wont allow any GUI editor to view your data. Some editors can decrypt the DB if you provide the password. The algorithm used is RSA.

Later if you wish to change the password, use

conn.ChangePassword("new_password");

To reset or remove password, use

conn.ChangePassword(String.Empty);

SQLite with encryption/password protection

SQLite has hooks built-in for encryption which are not used in the normal distribution, but here are a few implementations I know of:

  • SEE - The official implementation.
  • wxSQLite - A wxWidgets style C++ wrapper that also implements SQLite's encryption.
  • SQLCipher - Uses openSSL's libcrypto to implement.
  • SQLiteCrypt - Custom implementation, modified API.
  • botansqlite3 - botansqlite3 is an encryption codec for SQLite3 that can use any algorithms in Botan for encryption.
  • sqleet - another encryption implementation, using ChaCha20/Poly1305 primitives. Note that wxSQLite mentioned above can use this as a crypto provider.

The SEE and SQLiteCrypt require the purchase of a license.

Disclosure: I created botansqlite3.

Set password for SQLite v3 database

I followed the link which Wudge kindly appointed in comment above, and it works, but I'd rather clarify what need to be done:

  1. To set a password to an unprotected database:

    Dim conn = New SQLite.SQLiteConnection(
    "Data Source=C:\yourFolder\yourDB.db3;Version=3;")
    conn.Open()
    conn.ChangePassword("password")
    conn.Close()
  2. To open a password-protected database:

    Dim conn = New SQLite.SQLiteConnection(
    "Data Source=C:\yourFolder\yourDB.db3;Version=3;")
    conn.SetPassword("password")
    conn.Open()
    conn.Close()

    or

    Dim conn = New SQLite.SQLiteConnection(
    "Data Source=C:\yourFolder\yourDB.db3;Version=3;Password=password;")
    conn.Open()
    conn.Close()
  3. To remove password from a password-protected database:

    Dim conn = New SQLite.SQLiteConnection(
    "Data Source=C:\yourFolder\yourDB.db3;Version=3;Password=password;")
    conn.Open()
    conn.ChangePassword(String.Empty)
    conn.Close()

PS. The open source database manager SQLiteStudio is able to open files which were password-protected that way, as long as you choose System.Data.SQLite instead of Sqlite 3 as your database type. (Requires v 3.1.1, the current version).

How can I protect sqlite db in android from being stolen

You cannot complete protect your SQLlite DB as someone can reverse engineer your code and read the content of your protection mechanism.

You can take an alternate paths:

1) Make the DB online, i.e. don't put it with your app, have your product contact your DB and query it

2) Encrypt the data in the SQL Lite, and decrypt it on the fly inside your code - this will make it harder to just steal your SQL Lite, it is not prefect, but it is better than nothing



Related Topics



Leave a reply



Submit