Page 1 of 1

Help! How to Change Sqlite Database EncryptKey? About Sqlite

Posted: Sat 15 Jan 2011 07:24
by hlbzhx
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]

Posted: Mon 17 Jan 2011 08:14
by AlexP
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');

Posted: Mon 17 Jan 2011 10:52
by hlbzhx
OK! Thanks!

Test is Passed!!

Posted: Mon 17 Jan 2011 11:55
by AlexP
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.

Posted: Tue 18 Jan 2011 00:55
by hlbzhx
OK! It's very nice!

I wish the UniDac is getting better!

Posted: Tue 18 Jan 2011 01:11
by hlbzhx
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...

Posted: Tue 18 Jan 2011 09:56
by AlexP
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;

Posted: Tue 18 Jan 2011 10:23
by hlbzhx
OK! Thanks for your answer...

Posted: Tue 18 Jan 2011 10:35
by AlexP
Hello,

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

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

Posted: Tue 19 Mar 2013 11:22
by jota
Hi

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

I appreciate a small code example.

Regards

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

Posted: Tue 19 Mar 2013 14:34
by AlexP
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'

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

Posted: Wed 20 Mar 2013 12:51
by jota
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

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

Posted: Wed 20 Mar 2013 15:31
by AlexP
Hello,

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