No it did work, but I found the problem.

 

The problem is caused by no nonce / nonce situation.

 

If source database is created with nonce then there is no problem (the
pragma are not even needed) the backup database is properly encrypted.

 

If source database is created with no nonce (my case) then the backup
database get encrypted improperly.

 

Hope It can be fixed.

 

Thanks

 

Ulric

 

 

 

From: drhsql...@gmail.com [mailto:drhsql...@gmail.com] On Behalf Of Richard
Hipp
Sent: April 7, 2011 13:13
To: General Discussion of SQLite Database
Cc: Ulric Auger
Subject: Re: [sqlite] Bug: SQLite Encryption Extension And Backup API

 

 

On Wed, Apr 6, 2011 at 8:21 AM, Ulric Auger <ul...@gtechna.com> wrote:

Hi,

I think I found a bug creating an encrypted backup.



The source database is encrypted using key:

aes256:A94A8FE5CCB19BA61C4C0873D391E987982FBBD3



The source database is readable, I can run select queries and insert data
with no problem.



When I use the following function to create a backup of the database, the
backup database becomes unreadable.

I used the same encryption key as the source database for the backup one,
but when I try to read something from the backed up database I get an error
that the database is encrypted.



Thanks



int backupDb(sqlite3 *pDb, const char *zFilename, const char *zKey)

{

 int rc;

 sqlite3 *pBackupDb;

 sqlite3_backup *pBackup;



 rc = sqlite3_open(zFilename, &pBackupDb);

 if( rc==SQLITE_OK )

 {

   if (zKey != NULL)

       sqlite3_key(pBackupDb, zKey, strlen(zKey));


Please try doing something with pBackupDb here, after setting the key but
before overwriting with the backup.  Something like (for example):

    sqlite3_exec(pBackupDb, "PRAGMA secure_delete=OFF", 0, 0, 0);

We have lots and lots of test cases running backups into encrypted
databases, all of which work.  But after further investigation, I see that
we also run a series of pragmas (such as the one above) against the
destination database after the encryption key is set but before the backup
starts.

In the case I just now looked into, the following pragmas run:

    PRAGMA recursive_triggers=ON;
    PRAGMA foreign_keys=ON;
    PRAGMA secure_delete=OFF;

But probably any PRAGMA or any other statement (perhaps:  "SELECT * FROM
sqlite_master") will serve.

Please try this in your code and let me know if it clears the problem.

 




   pBackup = sqlite3_backup_init(pBackupDb, "main", pDb, "main");

   if( pBackup )

   {

     do

     {

       rc = sqlite3_backup_step(pBackup, 100);

       if( rc==SQLITE_OK || rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){

         sqlite3_sleep(10);

       }

     }

     while( rc==SQLITE_OK || rc==SQLITE_BUSY || rc==SQLITE_LOCKED );



     sqlite3_backup_finish(pBackup);

   }

   rc = sqlite3_errcode(pBackupDb);

 }



 sqlite3_close(pBackupDb);

 return rc;

}



Ulric Auger
--
Groupe Techna Inc.
 <mailto:ul...@gtechna.com> ul...@gtechna.com



_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users




-- 
D. Richard Hipp
d...@sqlite.org

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to