SQLite Android encryption

Discussion of open issues, suggestions and bugs regarding LiteDAC (SQLite Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Zvor
Posts: 7
Joined: Sun 26 Oct 2014 15:07

SQLite Android encryption

Post by Zvor » Sun 26 Oct 2014 15:15

Hello!
I have noticed the ability of using Direct mode on Android platform appeared in the most recent update of liteDAC! Does that mean encription is working too?
If not how is it possible to use third-party driver with liteDAC to get encription on Android using RAD Studio XE7?
Thanks

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

Re: SQLite Android encryption

Post by AlexP » Mon 27 Oct 2014 06:05

Hello,

Encryption is implemented by us in the Direct mode supported for Windows platforms only. For other platforms, you should use an external SQLite3 library with support for encryption. You can specify the SQLite3.dll location in the LiteConnection.ClientLibrary property.

Zvor
Posts: 7
Joined: Sun 26 Oct 2014 15:07

Re: SQLite Android encryption

Post by Zvor » Mon 27 Oct 2014 07:41

OK, many thanks, but how can i use alternative drivers on Android?
Should i use .dll or .so file?

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

Re: SQLite Android encryption

Post by AlexP » Mon 27 Oct 2014 09:27

For Android, libraries usually have the extension .so

Zvor
Posts: 7
Joined: Sun 26 Oct 2014 15:07

Re: SQLite Android encryption

Post by Zvor » Mon 27 Oct 2014 11:14

Can you show me some example how to connect to sqlite database using alternative library on Android?
I think about using SQLCipher open source but i don't see any .so files in it's distribution.
Maybe you can show with another library?

Thanks a lot

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

Re: SQLite Android encryption

Post by AlexP » Tue 28 Oct 2014 10:24

To work with third-party libraries, you should specify the path to the library in the LiteConnection.ClientLibrary property. We don't use and test our products on third-party libraries. You have to search for such libraries and test their functioning with LiteDAC.

Zvor
Posts: 7
Joined: Sun 26 Oct 2014 15:07

Re: SQLite Android encryption

Post by Zvor » Wed 29 Oct 2014 17:20

OK, thanks for your answer, one last question -
is it possible to use data encryption instead of database encryption on Android platform?

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

Re: SQLite Android encryption

Post by AlexP » Thu 30 Oct 2014 09:11

Yes, you may use data encryption in mobile applications. More details about data encryption can be found in the documentation: http://www.devart.com/litedac/docs/encryption.htm

Zvor
Posts: 7
Joined: Sun 26 Oct 2014 15:07

Re: SQLite Android encryption

Post by Zvor » Thu 30 Oct 2014 10:28

Thank you for your answer, but i don't quite understand how to use it.

I have SQlite database file with tables Log, Backup, text.

Code: Select all

CREATE TABLE [text] ([test] BINARY);
I plased components UniConnection, UniQuery and Encryptor with options:

Code: Select all

object SQLLocal: TUniConnection
    ProviderName = 'SQLite'
    SpecificOptions.Strings = (
      'SQLite.UseUnicode=True'
      'SQLite.EnableLoadExtension=True')
    Options.KeepDesignConnected = False
    LoginPrompt = False
end;

  object LocalDataSet: TUniQuery
    LocalUpdate = True
    Encryption.Fields = 'test'
    Encryption.Encryptor = UniEncryptor
    Connection = SQLLocal
    Options.FieldsOrigin = True
  end

  object UniEncryptor: TUniEncryptor
    Password = '1'
  end
Now on ButtonPress i do

Code: Select all

 localdataset.SQL.text:='INSERT INTO Text (test) VALUES (''123123 qwerty йцукен'')';
 UniEncryptor.Password := '1';
 localdataset.ExecSQL;

 localdataset.SQL.text:='SELECT * FROM text';
 localdataset.DataTypeMap.AddFieldNameRule ('test', ftwidestring);
 localdataset.Open;
and got error "Field: test String value is too long: <lots of hieroglyphs>".
If I don't use DataTypeMap no error occured but still i can open database with external database administration tool and see that my data looks like ''123123 qwerty йцукен''.

What am i doing wrong? Can Encryptor handle with cirillic symbols?

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

Re: SQLite Android encryption

Post by AlexP » Fri 31 Oct 2014 06:26

There is a sample of a console application demonstrating work with UniEncryption:

Code: Select all

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Uni,
  CREncryption,
  SQLiteUniProvider,
  windows;

var
  UniConnection: TUniConnection;
  UniQuery: TUniQuery;
  UniEncryption: TUniEncryptor;
begin
  UniConnection := TUniConnection.Create(nil);
  try
    UniConnection.ConnectString := 'ProviderName=SQLite;Data Source=:memory:;UseUnicode=true;LoginPrompt=False';
    UniConnection.Connect;
    UniConnection.ExecSQL('CREATE TABLE [test] ([txt] varchar2(2000))');
    UniEncryption := TUniEncryptor.Create(nil);
    UniEncryption.Password := '12345';
    try
      UniQuery := TUniQuery.Create(nil);
      try
        UniQuery.Connection := UniConnection;
        UniQuery.Encryption.Encryptor := UniEncryption;
        UniQuery.Encryption.Fields := 'txt';
        UniQuery.SQL.Text := 'select * from test';
        UniQuery.Open;
        UniQuery.Insert;
        UniQuery.Fields[0].AsString := 'test äöü эъя';
        UniQuery.Post;
        MessageBox(0, PChar(UniQuery.Fields[0].AsString), 'test', 0);
        UniQuery.Close;
        UniQuery.Encryption.Encryptor := nil;
        UniQuery.Open;
        MessageBox(0, PChar(UniQuery.Fields[0].AsString), 'test', 0);
        UniQuery.Close;
      finally
        UniQuery.Free;
      end;
    finally
      UniEncryption.Free;
    end;
  finally
    UniConnection.Free;
    readln;
  end;
end.

Post Reply