Is this .db encrypted?

Discussion of open issues, suggestions and bugs regarding LiteDAC (SQLite Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
instrumentally
Posts: 13
Joined: Thu 14 Aug 2014 16:05

Is this .db encrypted?

Post by instrumentally » Tue 06 Oct 2015 13:35

I have always been puzzled about LiteDAC with regards to this issue of encryption. I find it hard to believe that DevArt didn't build a simple procedure call to a table to query whether it is encrypted or not. To have to error trap one's source code in order to determine whether a table is encrypted or not seems odd. This is what I currently have to do to figure out whether a .db file is encrypted or not:

Code: Select all

  try
    LiteConnection.Open;
    FEncrypted:=true;
  except
    on E:Exception do
    begin
      msg:=E.Message;

      //try to open as unencrypted
      if (Pos('not a database', msg)>0) or (Pos('image is malformed', msg)>0) then
      begin
        liteConnection.EncryptionKey:='';
        try
          LiteConnection.Open;
          FEncrypted:=false;
        except
          on E:Exception do
            MessageDlg(E.ClassName + ': ' + E.Message, mtError, [mbOk], 0);
        end;
      end
      else
        MessageDlg(E.ClassName + ': ' + E.Message, mtError, [mbOk], 0);
    end;
  end;
The problem I've found with this is that exception message string has changed over time.

Is this the only way to determine if a .db is encrypted or not? To have to error trap and depend on a matching exception string?

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Is this .db encrypted?

Post by AlexP » Wed 07 Oct 2015 07:33

Hello,

Different error messages on an attempt to open an encrypted file were due to the fact that we encrypted file header in previous versions, and didn't in the latest versions. Instead of checking messages text, you can use the error codes:

Code: Select all

  SQLITE_NOTADB     = 26;   // File opened that is not a database file
  SQLITE_CORRUPT    = 11;   // The database disk image is malformed

Post Reply