Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a program that communicates with a SQLite database encrypted by a password. If I try with a db without any password it work, but with a database with password, after connecting, when executing the query it gives the error "File is not a database". Do you have any advice?

What I have tried:

C#
using Microsoft.Data.Sqlite;
using System;
using System.Data.SQLite;

...

string conn = "Data Source=C:\\Users\\Utente\\Desktop\\SQLite\\sqlite tools\\DB\\provapassword.db;";
SqliteConnection sql_conn = new SqliteConnection(conn);
sql_conn.Open();
Console.WriteLine("Connected to provapassword");
var cmd = sql_conn.CreateCommand();
cmd.CommandText = "SELECT quote($password);";
cmd.Parameters.AddWithValue("$password", "prova");
var quotedPassword = (string)cmd.ExecuteScalar();
cmd.CommandText = "PRAGMA key = " + quotedPassword;
cmd.Parameters.Clear();
cmd.ExecuteNonQuery();                  
try
{     
//INSERT
string query = "INSERT INTO nomi values(10, \"AAA\")";
cmd.CommandText = query;
if (cmd.ExecuteNonQuery() != 0)
    Console.WriteLine("OK");
else
    Console.WriteLine("Error");
sql_conn.Close();
Console.WriteLine("Connection close"); 
}
catch (Exception e)
{
   Console.WriteLine(e.Message.ToString());
}
Posted
Updated 26-Nov-19 2:15am

Unless you know the password, you can't access the file - that's the whole idea.
So if you try to open it without providing any password, or giving the wrong password the decryption fails, and you get a "not a database" error.

This may help: Encryption in Microsoft.Data.Sqlite | Brice’s Blog[^]
 
Share this answer
 
Comments
Member 14668576 26-Nov-19 5:05am    
Ok, but I know the password, this is "prova". But anyway this don't work
You have to supply the password to the database in the connection string, not a SELECT query.
Data Source=C:\\Users\\...\\DB\\provapassword.db;Password=password
 
Share this answer
 
Comments
Member 14668576 26-Nov-19 8:26am    
I've tried this too, but it gives me this error: "Cannot use "Password" connection string property: library was not built with encryption support, please see "https://www.sqlite.org/see" for more information"
I've tried both with Microsoft.Data.Sqlite and with System.Data.SQLite
Dave Kreskowiak 26-Nov-19 10:47am    
Well, after looking up Sqlite, it seems full database encryption is an unmitigated mess.

I assumed you were using Sqlite.NET, instead of looking at your using statements at the top of the code. The Password part of the connection string isn't supported by the Microsoft providers.

You can get sqlite.net from NuGet. Read here -> https://github.com/praeclarum/sqlite-net

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900