SQLite with encryption/password protection

后端 未结 9 1089
遇见更好的自我
遇见更好的自我 2020-11-22 07:45

I\'m just learning to use SQLite and I was curious if such is possible:

  1. Encryption of the database file?

  2. Password protect opening of the dat

相关标签:
9条回答
  • 2020-11-22 08:15

    Well, SEE is expensive. However SQLite has interface built-in for encryption (Pager). This means, that on top of existing code one can easily develop some encryption mechanism, does not have to be AES. Anything really. Please see my post here: https://stackoverflow.com/a/49161716/9418360

    You need to define SQLITE_HAS_CODEC=1 to enable Pager encryption. Sample code below (original SQLite source):

    #ifdef SQLITE_HAS_CODEC
    /*
    ** This function is called by the wal module when writing page content
    ** into the log file.
    **
    ** This function returns a pointer to a buffer containing the encrypted
    ** page content. If a malloc fails, this function may return NULL.
    */
    SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
      void *aData = 0;
      CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
      return aData;
    }
    #endif
    

    There is a commercial version in C language for SQLite encryption using AES256 - it can also work with PHP, but it needs to be compiled with PHP and SQLite extension. It de/encrypts SQLite database file on the fly, file contents are always encrypted. Very useful.

    http://www.iqx7.com/products/sqlite-encryption

    0 讨论(0)
  • 2020-11-22 08:16

    You can password protect SQLite3 DB. For the first time before doing any operations, set 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. Later if you wish to change the password, use conn.ChangePassword("new_password"); To reset or remove password, use conn.ChangePassword(String.Empty);

    0 讨论(0)
  • 2020-11-22 08:16

    Keep in mind, the following is not intended to be a substitute for a proper security solution.

    After playing around with this for four days, I've put together a solution using only the open source System.Data.SQLite package from NuGet. I don't know how much protection this provides. I'm only using it for my own course of study. This will create the DB, encrypt it, create a table, and add data.

    using System.Data.SQLite;
    
    namespace EncryptDB
    {
        class Program
        {
            static void Main(string[] args)
            {
                string connectionString = @"C:\Programming\sqlite3\db.db";
                string passwordString = "password";
                byte[] passwordBytes = GetBytes(passwordString);
                SQLiteConnection.CreateFile(connectionString);
                SQLiteConnection conn = new SQLiteConnection("Data Source=" + connectionString + ";Version=3;");
                conn.SetPassword(passwordBytes);
                conn.Open();
                SQLiteCommand sqlCmd = new SQLiteCommand("CREATE TABLE data(filename TEXT, filepath TEXT, filelength INTEGER, directory TEXT)", conn);
                sqlCmd.ExecuteNonQuery();
                sqlCmd = new SQLiteCommand("INSERT INTO data VALUES('name', 'path', 200, 'dir')", conn);
                sqlCmd.ExecuteNonQuery();
                conn.Close();
            }
            static byte[] GetBytes(string str)
            {
                byte[] bytes = new byte[str.Length * sizeof(char)];
                bytes = System.Text.Encoding.Default.GetBytes(str);
                return bytes;
            }
        }
    }
    

    Optionally, you can remove conn.SetPassword(passwordBytes);, and replace it with conn.ChangePassword("password"); which needs to be placed after conn.Open(); instead of before. Then you won't need the GetBytes method.

    To decrypt, it's just a matter of putting the password in your connection string before the call to open.

            string filename = @"C:\Programming\sqlite3\db.db";
            string passwordString = "password";
            SQLiteConnection conn = new SQLiteConnection("Data Source=" + filename + ";Version=3;Password=" + passwordString + ";");
            conn.Open();
    
    0 讨论(0)
提交回复
热议问题