Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Password Protect a SQLite DB. Is it possible?

Tags:

c#

sqlite

I have to face a new little project. It will have about 7 or 9 tables, the biggest of them will grow by a max rate of 1000 rows a month.

I thought about SQLite as my db... But i will need to protect the db in case anybody wants to change data from the db

My main question is:

Is it possible password protect a sqlite db as you would do on access?

The development would be on C#, but I'm searching something free.

like image 745
Jhonny D. Cano -Leftware- Avatar asked Sep 04 '09 19:09

Jhonny D. Cano -Leftware-


People also ask

Can I put password on SQLite database?

The base SQLite engine doesn't have any password/encryption options. You have to either use the paid SEE option for encryption, or some third party solution.

Can SQLite be encrypted?

SQLite doesn't support encrypting database files by default. Instead, you need to use a modified version of SQLite like SEE, SQLCipher, SQLiteCrypt, or wxSQLite3.

How secure is SQLite?

The SQLite Store is a set of database files, which is deployed on the untrusted area. However, data on the SQLite Store are protected with the authenticated encryption scheme, making data tampering and eavesdropping impossible.

How do I make SQLite secure?

One solution is to export/backup to a [well-defined] XML format or a "fake structured" SQLite database file. Or to encrypt the back-up database (e.g. in ZIP+password). Or just change the file extension (this will work for most people I'd imagine).


2 Answers

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); 
like image 186
Mangesh Avatar answered Oct 14 '22 22:10

Mangesh


You can use the built-in encryption of the sqlite .net provider (System.Data.SQLite). See more details at http://web.archive.org/web/20070813071554/http://sqlite.phxsoftware.com/forums/t/130.aspx

To encrypt an existing unencrypted database, or to change the password of an encrypted database, open the database and then use the ChangePassword() function of SQLiteConnection:

// Opens an unencrypted database SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3"); cnn.Open(); // Encrypts the database. The connection remains valid and usable afterwards. cnn.ChangePassword("mypassword"); 

To decrypt an existing encrypted database call ChangePassword() with a NULL or "" password:

// Opens an encrypted database SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3;Password=mypassword"); cnn.Open(); // Removes the encryption on an encrypted database. cnn.ChangePassword(null); 

To open an existing encrypted database, or to create a new encrypted database, specify a password in the ConnectionString as shown in the previous example, or call the SetPassword() function before opening a new SQLiteConnection. Passwords specified in the ConnectionString must be cleartext, but passwords supplied in the SetPassword() function may be binary byte arrays.

// Opens an encrypted database by calling SetPassword() SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3"); cnn.SetPassword(new byte[] { 0xFF, 0xEE, 0xDD, 0x10, 0x20, 0x30 }); cnn.Open(); // The connection is now usable 

By default, the ATTACH keyword will use the same encryption key as the main database when attaching another database file to an existing connection. To change this behavior, you use the KEY modifier as follows:

If you are attaching an encrypted database using a cleartext password:

// Attach to a database using a different key than the main database SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3"); cnn.Open(); cmd = new SQLiteCommand("ATTACH DATABASE 'c:\\pwd.db3' AS [Protected] KEY 'mypassword'", cnn); cmd.ExecuteNonQuery(); 

To attach an encrypted database using a binary password:

// Attach to a database encrypted with a binary key SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3"); cnn.Open(); cmd = new SQLiteCommand("ATTACH DATABASE 'c:\\pwd.db3' AS [Protected] KEY X'FFEEDD102030'", cnn); cmd.ExecuteNonQuery(); 
like image 21
Liron Levi Avatar answered Oct 14 '22 20:10

Liron Levi