Recent

Author Topic: SQLlite Encryption problem  (Read 5040 times)

kjteng

  • Sr. Member
  • ****
  • Posts: 259
SQLlite Encryption problem
« on: October 31, 2018, 02:49:24 am »
I just downloaded the new version of wxsqlite3.dll (ver 4.2.0) and have problem opening database previously created (with version3.5.8 encrypted with 128bit AES method).  I think this is because the new dll default encryption method is chacha20. 
My question: how can I change or set the encryption method to AES 128 so that I can read my old database?
 

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: SQLlite Encryption problem
« Reply #1 on: October 31, 2018, 06:54:13 am »
provided the dll is compiled with all supported ciphers:
You can use a PRAGMA for that.
See https://www.sqlite.org/see/doc/trunk/www/readme.wiki
Make sure you read the part here:
Quote
When using PRAGMA hexkey or PRAGMA hexrekey, the key prefix must be hex encoded just like the rest of the key.

PRAGMA hexkey='aes128:6d796b6579';         -- Wrong!!
PRAGMA hexkey='6165733132383a6d796b6579';  -- correct

What worries me a little is that chacha20 is according to that official documentation not supported by the official Sqlite SEE.
If the wxsqlite3.dll has its own format it may also have a different implementation of AES.
Then it may or may not work: it looks like it is not compiled from an official source.

OTOH it is claimed here http://wxcode.sourceforge.net/components/wxsqlite3/ and here https://github.com/utelle/wxsqlite3 that the encryption is compatible, so the PRAGMA should work too?
« Last Edit: October 31, 2018, 08:45:46 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

kjteng

  • Sr. Member
  • ****
  • Posts: 259
Re: SQLlite Encryption problem
« Reply #2 on: November 03, 2018, 06:06:58 am »
Thank you for the reply.
After some testings, I think the dll that I downloaded only supported chacha20. (Even for the earlier version I downloaded there are two separate version of dll: one for aes128 and another one for aes256).

Since I do not know how to compile the dll from the c++ source, I think the only way is to decrypt the existing database (using the old dll) and then encrypt it with the new dll ;-(

Please advise if there is alternative way to deal with this. Thanks again.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: SQLlite Encryption problem
« Reply #3 on: November 03, 2018, 06:24:13 am »
better ask on the wxsqlite forum or issue tracker
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

utelle

  • Newbie
  • Posts: 1
Re: SQLlite Encryption problem
« Reply #4 on: November 03, 2018, 06:04:56 pm »
After some testings, I think the dll that I downloaded only supported chacha20.

From where did you download the DLL? The DLLs available from https://github.com/utelle/wxsqlite3/releases/latest definitely support multiple ciphers (wxSQLite3 AES 128 Bit, wxSQLite3 AES 256 Bit, ChaCha20, and SQLCipher).

Quote from: kjteng
(Even for the earlier version I downloaded there are two separate version of dll: one for aes128 and another one for aes256).

Since wxSQLite3 version 4.0.0 there is only a single DLL which allows to select the cipher at runtime. The default cipher was changed from AES 128 Bit to ChaCha20, because the latter is more secure and incurs less runtime overhead. However, the DLL could be built using a different default cipher if necessary.

Quote from: kjteng
Since I do not know how to compile the dll from the c++ source, I think the only way is to decrypt the existing database (using the old dll) and then encrypt it with the new dll ;-(

Please advise if there is alternative way to deal with this.

Since wxSQLite3 4.x supports the ciphers AES 128 Bit and AES 256 Bit of the pre-4.x versions of wxSQLite3, it is not necessary to decrypt and reencrypt your databases. However, you will have to adjust the cipher parameters, before issuing "PRAGMA key='...';

A description, how the cipher parameters can be adjusted can be found here: https://github.com/utelle/wxsqlite3/tree/master/sqlite3secure.

Regards,

Ulrich

P.S.: The ciphers of the official SQLite3 Encryption Extension (https://www.sqlite.org/see) are not supported by wxSQLite3. The reason is that SEE is a commercial extension with closed source.

kjteng

  • Sr. Member
  • ****
  • Posts: 259
Re: SQLlite Encryption problem
« Reply #5 on: November 05, 2018, 02:58:52 pm »
Many thanks your detailed explanations  (in this forum as well as https://forums.wxwidgets.or
I think I can replace the old version with new one now.
For the benefit of other members who may be interested, I have summarised below what I have learned for wxsqlite3.4.2.0 (using lazarus with zeolib db component):-

1. before connecting to the database -
     i) set the encrypted = 'True' (if database is encrypted, otherwise set this to '')
        or  set ZConnection1.Properties.Values['encrypted'] := 'True' // or 'False' as  the cas may be
     ii) set ZConnection1.Password
     iii) call ZConnection1.Connect

2. If the database was not encryted, you should be able to operate it as usual now.
    If the database was encryted, you must activate the encryption method by calling -
         select wxsqlite3_config(cipherName, cipherParameter, newValue);
   For the values of cipherName, cipherParameter, newValue, please refer to the files in
    ...\sqlite3secure\test\folder
   For example, if the database was encrypted in with AES256, check the file setaes256wx.sql
   and then call
      ZConnection1.ExecuteDirect('SELECT wxsqlite3_config("cipher", "aes256cbc");');
      ZConnection1.ExecuteDirect('SELECT wxsqlite3_config("aes256cbc", "legacy", 0);');   
       ....
3. Finally, call pragma key="password" eg -
     ZConnection1.ExecuteDirect('PRAGMA key="12345"' ); // say, 12345 is the password
   With this, I can operate the database tables as usual now

Note:
 Sstep 2 & 3 is not necessary if the database is encryted with default method i.e. chacha20.
       


 

TinyPortal © 2005-2018