in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

Encrypting, decrypting and attaching to encrypted databases

Last post 12-15-2011 7:24 AM by madrox. 36 replies.
Page 3 of 3 (37 items) < Previous 1 2 3
Sort Posts: Previous Next
  • 01-11-2011 7:35 AM In reply to

    Re: Encrypting, decrypting and attaching to encrypted databases

    The easiest way to see if the data is encrypted or not is to use the sqlite3.exe command-line program, and try and open the database.  If it says "encrypted or not a database" when you try and access it, then you'll know it's encrypted.

     

  • 01-11-2011 9:37 PM In reply to

    Re: Encrypting, decrypting and attaching to encrypted databases

    Thank you for reply. I already tried that and its showing  "encrypted or not a database" when I open database. I know that database is password protected and encrypted, but how can I see encrypted data?

  • 01-12-2011 7:32 AM In reply to

    Re: Encrypting, decrypting and attaching to encrypted databases

    You can't.  When you apply encryption to the database file, everything is encrypted.  The schema, the data, everything.  SQLite will not open the database at all unless a password is supplied.

    If you want to verify that the database is truly encrypted, then open it with a hex editor and look for some clear text.  You shouldn't find any.

     

  • 01-27-2011 5:33 AM In reply to

    Re: Encrypting, decrypting and attaching to encrypted databases

    How to attach an unencrypted database to an encrypted?

    I have encrypted db 'enc.db3' (with key '1234') and unencrypted 'unenc.db3'.


    Query:
    attach 'c:\unenc.db3' as tst KEY '' --on db 'enc.db3'
    wan't work, always return "File is encrypted or not a database."

    Queries:
    attach 'c:\unenc.db3' as tst KEY '' --on db 'unenc.db3'
    attach 'c:\enc.db3' as tst KEY '1234' --on db 'enc.db3'
    attach 'c:\enc.db3' as tst KEY '1234' --on db 'unenc.db3'
    works good
  • 06-12-2011 2:59 PM In reply to

    Re: Encrypting, decrypting and attaching to encrypted databases

    Hi, I wrote a little script to change the password interactively. Hope this helps in speeding up your work.

    using System;
    using System.Data.SQLite;
    
    public class SQLitePasswd
    {
    	public static void Main()
    	{
    		string connstring = "";
    		string newpasswd = "";
    		SQLiteConnection cnn = null;
    
    		System.Console.Write("Enter the connection string: ");
    		connstring = System.Console.ReadLine();
    
    		System.Console.Write("Enter the new password (empty to remove): ");
    		newpasswd = System.Console.ReadLine();
    
    		try
    		{
    			cnn = new SQLiteConnection(connstring);
    			cnn.Open();
    			if (newpasswd.Length > 0)
    			{
    				cnn.ChangePassword(newpasswd);
    				System.Console.WriteLine("Password changed");
    			}
    			else
    			{
    				cnn.ChangePassword((String)null);
    				System.Console.WriteLine("Password removed");
    			}
    
    		}
    		catch (SQLiteException exc)
    		{
    			System.Console.WriteLine("Caught exception: " + exc.Message);
    			System.Environment.Exit(1);
    		}
    		finally 
    		{
    			if(cnn != null && cnn.State != System.Data.ConnectionState.Closed)
    				cnn.Close();
    		}
    	}
    
    }
    
    
  • 06-13-2011 1:10 PM In reply to

    • huji
    • Not Ranked
    • Joined on 06-13-2011
    • Posts 2

    Re: Encrypting, decrypting and attaching to encrypted databases

     Thank you for sharing the code, but I have a problem running it. When the program calls this function: 

    cnn.ChangePassword(newpasswd);

     I get this error message:

     Unable to find an entry point named 'sqlite3_rekey' in DLL 'sqlite3'.

     I am using the sqlite3.dll from here: http://www.sqlite.org/download.html

     The link I use to download it is title sqlite-dll-win32-x86-3070603.zip and the DLL is 553 KB. Am I using the wrong DLL?

    cosenmarco:

    ...

     

    		try
    		{
    			cnn = new SQLiteConnection(connstring);
    			cnn.Open();
    			if (newpasswd.Length > 0)
    			{
    				cnn.ChangePassword(newpasswd);
    				System.Console.WriteLine("Password changed");
    			}
    			else
    			{
    				cnn.ChangePassword((String)null);
    				System.Console.WriteLine("Password removed");
    			}
    
    		}

    ...


     

  • 12-15-2011 7:24 AM In reply to

    • madrox
    • Not Ranked
    • Joined on 12-15-2011
    • Posts 1

    Re: Encrypting, decrypting and attaching to encrypted databases

    Hi everyone!

    I am running Visual Studio 2010 on a Windows 7 x64. The app I'm writing is supposed to run on all platforms Any CPU). I'm able to encrypt/decrypt the database file if I'm using the System.Data.SQLite.dll either x86 version (sqlite-netFx40-static-binary-bundle-Win32-2010-1.0.77.0.zip) or x64 one (sqlite-netFx40-static-binary-bundle-x64-2010-1.0.77.0.zip).

    I need my app to run on both platforms x86 and x64 (Any CPU project build setting). I've tried installing the ADO.NET 4.0 Provider (SQLite-1.0.67.1-vs2010-net4-setup.exe). I added the reference (right-click on project, Add Reference, .NET tab -> System.Data.SQLite) and ran the program. If I decrypt the file and try to encrypt by calling ChangePassword("myPass"), I get the following exception:

    System.EntryPointNotFoundException was caught
      Message=Unable to find an entry point named 'sqlite3_rekey' in DLL 'System.Data.SQLite.DLL'.
      Source=System.Data.SQLite
      TypeName=""
      StackTrace:
           at System.Data.SQLite.UnsafeNativeMethods.sqlite3_rekey(IntPtr db, Byte[ key, Int32 keylen)
           at System.Data.SQLite.SQLite3.ChangePassword(Byte[ newPasswordBytes)
           at System.Data.SQLite.SQLiteConnection.ChangePassword(Byte[ newPassword)
           at System.Data.SQLite.SQLiteConnection.ChangePassword(String newPassword)
           at SQLiteTest.Database.Encrypt() in C:\SQLiteTest\Database.cs:line 166
      InnerException:

    Also, I've tried to open a connection using the SQLiteConnection object and I get two different exceptions in two different cases.
    First, if the file is encrypted and I don't specify a password in the connection string I get this:

    System.Data.SQLite.SQLiteException was caught
      Message=File opened that is not a database file
    file is encrypted or is not a database
      Source=System.Data.SQLite
      ErrorCode=-2147467259
      StackTrace:
           at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous,

    UInt32 timeoutMS, String& strRemain)
           at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
           at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
           at System.Data.SQLite.SQLiteDataReader.NextResult()
           at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
           at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
           at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
           at System.Data.SQLite.SQLiteConnection.Open()
           at SQLiteTest.Database.GetAllLanguages() in C:\SQLiteTest\Database.cs:line 216
      InnerException:

    Second, if I add the password parameter to the connection string, I get the System.EntryPointNotFoundException like the one above.

    Maybe I got the wrong impression that the ADO.NET provider is supposed to help with this.
    Please help, guys!

Page 3 of 3 (37 items) < Previous 1 2 3
Powered by Community Server (Commercial Edition), by Telligent Systems