17

I am using SQLite database version 3 with C# Windows application.. i want to encrypt the SQLite database file using password or any other encryption way in order to prevent clients to open it from program files folder.

i don't want any runtime encryption ways, i just want to make the database file show password field when a client try to open it from the program files .. thanks

Edit

and if i encrypted it from code,the client can open it when the installation complete and db file transferred to the program files before opening the program to perform the encryption isnt it?

Community
  • 1
  • 1
Hassanation
  • 866
  • 2
  • 14
  • 29

5 Answers5

24

I use SQLite version 3 works perfectly! You have to do that:

//if the database has already password
try{
            string conn = @"Data Source=database.s3db;Password=Mypass;";
            SQLiteConnection connection= new SQLiteConnection(conn);
            connection.Open();
            //Some code
            connection.ChangePassword("Mypass");
            connection.Close();
    }
//if it is the first time sets the password in the database
catch
    {
            string conn = @"Data Source=database.s3db;";
            SQLiteConnection connection= new SQLiteConnection(conn);
            connection.Open();
            //Some code
            connection.ChangePassword("Mypass");
            connection.Close();
    }

thereafter if the user tries to open the database. will say protected by Admin or the database is encrypted or is not a database or corrupted file!

Luis Granado
  • 308
  • 3
  • 7
  • if i apply this code on some sqlite database and it get encypted, what if i want to edit some table in database manually, i mean is there any way to decrypt it back ? what code i need to write for that ? – Mogli Apr 25 '14 at 09:05
  • I think there is no other way just code. what you can do is make a standalone application with this code puts the password empty and asks the old password to access it – Luis Granado Apr 25 '14 at 10:42
  • i used this con.Open();con.ChangePassword("Mypass"); con.Close(); now it is encrypted now i want to change it manually and want to open it what should i do ? – Mogli May 08 '14 at 14:52
  • I set the password at `SQLiteConnection.CreateFile()` time – Jack Jun 08 '16 at 19:54
  • This worked for me. Thanks. But the drawback is, if you know the password still you cant open the DB file. Is there any way to view the protected DB file? – Saikat Chakraborty Apr 20 '18 at 11:45
3

Found in this forum an post indicating that ..

Standard SQLite3 API doesn't offer any form of protection and relies only on underlying OS privileges mecanism (if any) for "security". If you have an existing SQLite-style database which uses a specific API to gain access, then you should use this particular (non-standard) API.

If you can/want to use some kind of extension for SQLite you can also try SQLite Encryption Extension (SEE) or SQLite Crypt

But you can change/set a password for your database using SQLite.Data as shown in this article.

Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
  • if i used some kind of extension for it, will the connection work well as it is with the same extension ? – Hassanation Aug 30 '12 at 06:18
  • you mean if you use some extension to encrypt, if it is still possible to create and open a connection to this db from within c#? – Pilgerstorfer Franz Aug 30 '12 at 06:33
  • 1
    Did some research. Unfortunatelly I did not found any solution how to access SQLite Crypt or SEE within a .Net app. Those extensions are built in C and no COM interface available. I think you should give SQLite.Data a chance, and check if it is possible to open a db from your disk when it has been encrypted with it. – Pilgerstorfer Franz Aug 30 '12 at 08:20
  • i have found a solution for my problem>> i will encrypt the rows data inside the data table, and then call the encrypted data in code and pass it to the decrypt method to decrypt it>> so any one can open the SQLite file but wont know what exactly it means. any feedback ? :) – Hassanation Aug 30 '12 at 09:56
  • 1
    If you really need to secure your dataBase and/or your data I would use SQLite.Data and set a password for the database. Within your tables I only would encrypt those fields that contain sensitive data. Anything else is unnecessary effort imo. – Pilgerstorfer Franz Aug 30 '12 at 10:00
3

Try sqlitestudio.pl as editor.
For Database Type, choose System.Data.SQLite while connecting.

Skandix
  • 1,916
  • 6
  • 27
  • 36
1

Unfortunately, password in SQlite file can be added or removed or changed only from code. For that you need System.Data.SQLite namespace which will give you methods as well as Adapters and connections stuff to do so.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0

You can use the built-in encryption of the sq-lite .net provider (System.Data.SQLite). try this:

http://sqlite.phxsoftware.com/forums/t/130.aspxenter link description here

Or use:

SQLiteCrypt API

KF2
  • 9,887
  • 8
  • 44
  • 77