Help! How to Change Sqlite Database EncryptKey? About Sqlite

Help! How to Change Sqlite Database EncryptKey? About Sqlite

Postby 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]
hlbzhx
 
Posts: 19
Joined: Tue 03 Nov 2009 03:29

Postby 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');
AlexP
Devart Team
 
Posts: 3814
Joined: Tue 10 Aug 2010 11:35

Postby hlbzhx » Mon 17 Jan 2011 10:52

OK! Thanks!

Test is Passed!!
hlbzhx
 
Posts: 19
Joined: Tue 03 Nov 2009 03:29

Postby 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.
AlexP
Devart Team
 
Posts: 3814
Joined: Tue 10 Aug 2010 11:35

Postby 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

Postby 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...
hlbzhx
 
Posts: 19
Joined: Tue 03 Nov 2009 03:29

Postby 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;
AlexP
Devart Team
 
Posts: 3814
Joined: Tue 10 Aug 2010 11:35

Postby hlbzhx » Tue 18 Jan 2011 10:23

OK! Thanks for your answer...
hlbzhx
 
Posts: 19
Joined: Tue 03 Nov 2009 03:29

Postby AlexP » Tue 18 Jan 2011 10:35

Hello,

If any other questions come up, please contact us again.
AlexP
Devart Team
 
Posts: 3814
Joined: Tue 10 Aug 2010 11:35

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

Postby 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
jota
 
Posts: 22
Joined: Tue 22 Nov 2011 19:21

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

Postby 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'
AlexP
Devart Team
 
Posts: 3814
Joined: Tue 10 Aug 2010 11:35

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

Postby 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
jota
 
Posts: 22
Joined: Tue 22 Nov 2011 19:21

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

Postby 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.
AlexP
Devart Team
 
Posts: 3814
Joined: Tue 10 Aug 2010 11:35


Return to Universal Data Access Components