Help! How to Change Sqlite Database EncryptKey? About Sqlite

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
hlbzhx
Posts: 19
Joined: Tue 03 Nov 2009 03:29

Help! How to Change Sqlite Database EncryptKey? About Sqlite

Post by hlbzhx » Sat 15 Jan 2011 07:24

Quote before topic...
--------------------------------------------------------------------------------------
AlexP wrote:Hello,


We have added encryption support with using standard SQLite functions: sqlite3_key, sqlite3_rekey.
You can use these features like:

use the key
UniConnection1.SpecificOptions['EncryptionKey']:= 'key'

set the key
uses LiteClassesUni;
procedure EncryptDatabase(Connection: TCRConnection; NewKey: _string);

but if your sqlite.dll doesn't support encryption you can't use these features.

We will add this information into the UniDAC help in one of the next builds.
------------------------------------------------------------------------------

Question One:
use the key
UniConnection1.SpecificOptions['EncryptionKey']:= 'key'

Is Wrong, Correct Is :

Code: Select all

 UniConnection1.SpecificOptions.Values['EncryptionKey']:='key'
Question Two:
How to Change Encrypt Key to A New Key!
procedure EncryptDatabase(Connection: TCRConnection; NewKey: _string);

I How to Use this procedure, How to Get TCRConnection type From TUniConnection In my Application ! how to use Connection parameter !

use UniConnection1 to Connect Sqlite Database In my Application, How to Change Encrypt Key

Give me a sample for change encryptkey is very nice.

Thanks !!!

[/code]

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

Post by AlexP » Mon 17 Jan 2011 08:14

Hello,

Yes, you are right, the correct syntax is

UniConnection1.SpecificOptions.Values['EncryptionKey']:='key'

Sorry for my misprints.

You should use the following syntax to change the encryption key:

EncryptDatabase(TUniUtils.GetCRConnection(UniConnection1), 'new_key');

hlbzhx
Posts: 19
Joined: Tue 03 Nov 2009 03:29

Post by hlbzhx » Mon 17 Jan 2011 10:52

OK! Thanks!

Test is Passed!!

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

Post by AlexP » Mon 17 Jan 2011 11:55

Hello,

We have decided to simplify the usage of the encryption method in SQLite, so we have changed the EncryptDatabase method like:

TLiteUtils.EncryptDatabase(UniConnection1, 'key');

and moved it to the TLiteUtils class in the LiteServicesUni module.

This change will be included in the next build of UniDAC.

hlbzhx
Posts: 19
Joined: Tue 03 Nov 2009 03:29

Post by hlbzhx » Tue 18 Jan 2011 00:55

OK! It's very nice!

I wish the UniDac is getting better!

hlbzhx
Posts: 19
Joined: Tue 03 Nov 2009 03:29

Post by hlbzhx » Tue 18 Jan 2011 01:11

I Recommended the Procedure EncryptDataBase Can be Changed to Function EncryptDataBase ??

have a Boolean Type return determine Success or Fail of this Function EncryptDataBase...


say sorry, My english is poor...

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

Post by AlexP » Tue 18 Jan 2011 09:56

Hello,

If an error arises when you try to call the EncryptDatabase method, then you've got the correct SQLite exception that you can handle in the try..except block like

try
TLiteUtils.EncryptDatabase(UniConnection1,'key');
except
on e:ESQLiteError do
ShowMessage(e.Message+';'+e.ClassName+':'+IntToStr(e.ErrorCode));
end;

hlbzhx
Posts: 19
Joined: Tue 03 Nov 2009 03:29

Post by hlbzhx » Tue 18 Jan 2011 10:23

OK! Thanks for your answer...

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

Post by AlexP » Tue 18 Jan 2011 10:35

Hello,

If any other questions come up, please contact us again.

jota
Posts: 34
Joined: Tue 22 Nov 2011 19:21

Re: Help! How to Change Sqlite Database EncryptKey? About Sqlite

Post by jota » Tue 19 Mar 2013 11:22

Hi

Actually, with UniDAC, what would be the process?
Previously the database must be connected?

I appreciate a small code example.

Regards

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

Re: Help! How to Change Sqlite Database EncryptKey? About Sqlite

Post by AlexP » Tue 19 Mar 2013 14:34

Hello,

The following samples demonstrate work with encrypted DBs

Encrypt a database

Code: Select all

UniConnection1.Database := 'C:\sqlite.db3';                                   // the name of the database to be encrypted
UniConnection1.SpecificOptions.Values['ForceCreateDataBase'] := 'False';      // to check that the database exists
UniConnection1.SpecificOptions.Values['Direct'] := 'True';
UniConnection1.SpecificOptions.Values['EncryptionAlgorithm'] := 'laBlowfish'; // the database will be encrypted with the Blowfish encryption algorithm
UniConnection1.SpecificOptions.Values['EncryptionKey'] := '';                 // no encryption key specified, because the database is not encrypted yet
UniConnection1.Open;                                                          // connect to the database
TLiteUtils.EncryptDatabase(UniConnection1, '11111');                          // encrypt the database using the "11111" encryption key
Creating of a new encrypted database

Code: Select all

UniConnection1.Database : = 'C:\sqlite_encoded.db3';                          // the name of the database to be created
UniConnection1.SpecificOptions.Values['ForceCreateDataBase'] := 'True';       // this will allow to create the new database
UniConnection1.SpecificOptions.Values['Direct'] := 'True';
UniConnection1.SpecificOptions.Values['EncryptionAlgorithm'] := 'laBlowfish'; // the database will be encrypted with the Blowfish encryption algorithm
UniConnection1.SpecificOptions.Values['EncryptionKey'] := '11111';            // the encryption key for the database
UniConnection1.Open;                                                          // create and connect to the database
Connecting to an encrypted database

Code: Select all

UniConnection1.Database : = 'C:\sqlite_encoded.db3';                          // the name of the database to connect to
UniConnection1.SpecificOptions.Values['ForceCreateDataBase'] := 'False';      // to check that the database exists
UniConnection1.SpecificOptions.Values['Direct'] := 'True';                    // database file encryption is supported in the Direct mode only
UniConnection1.SpecificOptions.Values['EncryptionAlgorithm'] := 'laBlowfish'; // the encryption algorithm of the database
UniConnection1.SpecificOptions.Values['EncryptionKey'] := '11111';            // the encryption key for the database
UniConnection1.Open;
Changing the encryption key for the database

Code: Select all

UniConnection1.Database : = 'C:\sqlite_encoded.db3';                          // the name of the database to connect to
UniConnection1.SpecificOptions.Values['ForceCreateDataBase'] := 'False';      // to check that the database exists
UniConnection1.SpecificOptions.Values['Direct'] := 'True';
UniConnection1.SpecificOptions.Values['EncryptionAlgorithm'] := 'laBlowfish'; // the encryption algorithm of the database
UniConnection1.SpecificOptions.Values['EncryptionKey'] := '11111';            // the encryption key for the database
UniConnection1.Open;                                                          // connect to the database
TLiteUtils.EncryptDatabase(UniConnection1, '11111');                          // change the database encryption key to '22222'

jota
Posts: 34
Joined: Tue 22 Nov 2011 19:21

Re: Help! How to Change Sqlite Database EncryptKey? About Sqlite

Post by jota » Wed 20 Mar 2013 12:51

Hello AlexP

Thanks you very much for your examples.

I used the code "Changing the encryption key for the database".

This code runs successfully, but i can´t open the database with the new password, except if the new password is only one digit, then i can open the database after the change.

Other data:

Delphi XE2
UniDAC ver. 4.3.8
Unit for TLiteUtils.EncryptDatabase -> SQLiteUniProvider

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

Re: Help! How to Change Sqlite Database EncryptKey? About Sqlite

Post by AlexP » Wed 20 Mar 2013 15:31

Hello,

We cannot reproduce the problem, please try to reproduce the problem on the latest UniDAC version 4.6.12.

Post Reply