DB Error: 26 "file is encrypted or is not a database"

1,671 views
Skip to first unread message

Nico VZ

unread,
Jul 24, 2014, 3:21:07 AM7/24/14
to sqlc...@googlegroups.com
Hi there, 

This week i purchased the sqlCypher for iOS.
I got the library and setup a test project just for testing pusposes. (followed this tutorial: http://sqlcipher.net/sqlcipher-binaries-ios-and-osx)
For my project i need to use sqlcypher with FMDatabase. I found some topics about this and gave this tutorial a try: http://www.guilmo.com/fmdb-with-sqlcipher-tutorial/
I created a database, encrypted it with his code and tried to query but it always outputs the message `DB Error: 26 "file is encrypted or is not a database`
The code i used to open the encrypted database is: 

FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
[db open];
BOOL keyIsSet
= [db setKey:@"testEncryptionKey"];
if(keyIsSet){
 
NSLog(@"Key seems to be correct for database:%@", dbPath);
}
else{
 
NSLog(@"Key is incorrect.. ");
}

FMResultSet *rs = [db executeQuery:@"SELECT * FROM testtable"];

if ([rs next]){
 
// do something with the data
}
[rs close];
[db close];


First i thought there was something wrong with setting the encryption so i tried to set the encryption key with my SQLiteManager (http://www.sqlabs.com/sqlitemanager.php) program. (see attached screenshot, seems to be correct right?)
But that did not work also, still getting the error that says that the database file is not OK. The little check i built in my script above outputs that the key and path to the database is correct. but still the error message appears. 

What am i missing?

Schermafbeelding 2014-07-24 om 09.17.45.png

Nick Parker

unread,
Jul 24, 2014, 9:35:39 AM7/24/14
to sqlc...@googlegroups.com
Hello Nico,

Is the database you are attempting to open with the call to
[FMDatabaseWithPath:dbPath] already encrypted, or is it a plain text
database you wish to encrypt? If the latter, you will need to export
the plain text database to an encrypted database first, an example of
this is example #1 [1] using the sqlcipher_export function.

[1] http://sqlcipher.net/sqlcipher-api/#sqlcipher_export

On 7/24/14 2:21 AM, Nico VZ wrote:
> Hi there,
>
> This week i purchased the sqlCypher for iOS.
> I got the library and setup a test project just for testing pusposes.
> (followed this tutorial:
> http://sqlcipher.net/sqlcipher-binaries-ios-and-osx)
> For my project i need to use sqlcypher with FMDatabase. I found some
> topics about this and gave this tutorial a try:
> http://www.guilmo.com/fmdb-with-sqlcipher-tutorial/
> I created a database, encrypted it with his code and tried to query but
> it always outputs the message `DB Error: 26 "file is encrypted or is not
> a database`
> The code i used to open the encrypted database is:
>
> |
> FMDatabase*db =[FMDatabasedatabaseWithPath:dbPath];
> [db open];
> BOOL keyIsSet =[db setKey:@"testEncryptionKey"];
> if(keyIsSet){
> NSLog(@"Key seems to be correct for database:%@",dbPath);
> }
> else{
> NSLog(@"Key is incorrect.. ");
> }
>
> FMResultSet*rs =[db executeQuery:@"SELECT * FROM testtable"];
>
> if([rs next]){
> // do something with the data
> }
> [rs close];
> [db close];
> |
>
>
> First i thought there was something wrong with setting the encryption so
> i tried to set the encryption key with my SQLiteManager
> (http://www.sqlabs.com/sqlitemanager.php) program. (see attached
> screenshot, seems to be correct right?)
> But that did not work also, still getting the error that says that the
> database file is not OK. The little check i built in my script above
> outputs that the key and path to the database is correct. but still the
> error message appears.
>
> What am i missing?
>
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "SQLCipher Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to sqlcipher+...@googlegroups.com
> <mailto:sqlcipher+...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

--
Nick Parker

signature.asc

Nico VZ

unread,
Jul 25, 2014, 3:08:56 AM7/25/14
to sqlc...@googlegroups.com
Hi Nick, 

The database is already encrypted.
I have tried two methods for encrypting and the database is succesfully encrypted in both ways (i have checked it with SQLiteManager, it asks for a password before opening)
so we can presume it is encrypted correctly, but why doesn't FMDB open it when i use setKey?

Op donderdag 24 juli 2014 09:21:07 UTC+2 schreef Nico VZ:

Nick Parker

unread,
Jul 25, 2014, 9:08:09 AM7/25/14
to sqlc...@googlegroups.com
Hello,

My initial thought is that SQLCipher is not properly integrated
somewhere within your project. As an experiment, could you run the
following command, it will return a single string that represents the
SQLCipher version:

PRAGMA cipher_version;

If you do not receive a result, there is likely a project configuration
issue at hand. Please let us know what it reports.

On 7/25/14 2:08 AM, Nico VZ wrote:
> Hi Nick,
>
> The database is already encrypted.
> I have tried two methods for encrypting and the database is succesfully
> encrypted in both ways (i have checked it with SQLiteManager, it asks
> for a password before opening)
> so we can presume it is encrypted correctly, but why doesn't FMDB open
> it when i use setKey?
>
> Op donderdag 24 juli 2014 09:21:07 UTC+2 schreef Nico VZ:
>
> Hi there,
>
> This week i purchased the sqlCypher for iOS.
> I got the library and setup a test project just for testing
> pusposes. (followed this tutorial:
> http://sqlcipher.net/sqlcipher-binaries-ios-and-osx
> <http://sqlcipher.net/sqlcipher-binaries-ios-and-osx>)
> For my project i need to use sqlcypher with FMDatabase. I found some
> topics about this and gave this tutorial a try:
> http://www.guilmo.com/fmdb-with-sqlcipher-tutorial/
> <http://www.guilmo.com/fmdb-with-sqlcipher-tutorial/>
> I created a database, encrypted it with his code and tried to query
> but it always outputs the message `DB Error: 26 "file is encrypted
> or is not a database`
> The code i used to open the encrypted database is:
>
> |
> FMDatabase*db =[FMDatabasedatabaseWithPath:dbPath];
> [db open];
> BOOL keyIsSet =[db setKey:@"testEncryptionKey"];
> if(keyIsSet){
> NSLog(@"Key seems to be correct for database:%@",dbPath);
> }
> else{
> NSLog(@"Key is incorrect.. ");
> }
>
> FMResultSet*rs =[db executeQuery:@"SELECT * FROM testtable"];
>
> if([rs next]){
> // do something with the data
> }
> [rs close];
> [db close];
> |
>
>
> First i thought there was something wrong with setting the
> encryption so i tried to set the encryption key with my
> SQLiteManager (http://www.sqlabs.com/sqlitemanager.php
> <http://www.sqlabs.com/sqlitemanager.php>) program. (see attached
> screenshot, seems to be correct right?)
> But that did not work also, still getting the error that says that
> the database file is not OK. The little check i built in my script
> above outputs that the key and path to the database is correct. but
> still the error message appears.
>
> What am i missing?
>
signature.asc

Nico VZ

unread,
Jul 25, 2014, 9:55:33 AM7/25/14
to sqlc...@googlegroups.com
Hi Nick, 

When i run the query i get the following result: 3.1.0
Seems to be correct right?

Met vriendelijke groet,

Nico van Zijl

Nick Parker

unread,
Jul 25, 2014, 10:04:17 AM7/25/14
to sqlc...@googlegroups.com
Hello Nico,

Ok, great - that is a good sign! Next, do you know what version of
SQLCipher was used to create the encrypted database you are trying to
open with FMDB? Have you also tried to open the encrypted database via
the SQLCipher shell? Are you certain the database was encrypted with
SQLCipher to begin with?

On 7/25/14 8:55 AM, Nico VZ wrote:
> Hi Nick,
>
> When i run the query i get the following result: *3.1.0*
> Seems to be correct right?
>
> Met vriendelijke groet,
>
> Nico van Zijl
>
>
> 2014-07-25 15:08 GMT+02:00 Nick Parker <npa...@zetetic.net
> <mailto:npa...@zetetic.net>>:
> <mailto:sqlcipher%2Bunsu...@googlegroups.com>
> > <mailto:sqlcipher+...@googlegroups.com
> <mailto:sqlcipher%2Bunsu...@googlegroups.com>>.
signature.asc

Nico VZ

unread,
Jul 25, 2014, 10:15:44 AM7/25/14
to sqlc...@googlegroups.com
Hi Nick, 

In my first attempt i created a database in my SQLiteManager tool (version 4.3.0, released July 07, 2014), i do not know what sqlcypher version it uses for encryption. (i presume it is a pretty recent version because the sqlcypher encryption is added in 4.3.0) 

The second attempt was to use the code of http://www.guilmo.com/fmdb-with-sqlcipher-tutorial/. In this attempt i did not load the sqlcyher from the cocoapods like the tutorial. i just used his code to encrypt the database (the part below "Working the magic of SQLCipher")

Met vriendelijke groet,

Nico van Zijl


Nick Parker

unread,
Jul 25, 2014, 10:23:37 AM7/25/14
to sqlc...@googlegroups.com
Hello Nico,

Just to rule out any particular issues you might be seeing, could you
try building the SQLCipher shell from source? It sounds as if you are
running on a Mac, the instruction are fairly straightforward:

http://sqlcipher.net/introduction/

Once you have done that, could you try opening the database from the
command line:

./sqlcipher /path/to/database.db
PRAGMA key = 'YourKeyHere';
.s

On 7/25/14 9:15 AM, Nico VZ wrote:
> Hi Nick,
>
> In my first attempt i created a database in my SQLiteManager tool
> (version 4.3.0, released July 07, 2014), i do not know what sqlcypher
> version it uses for encryption. (i presume it is a pretty recent version
> because the sqlcypher encryption is added in 4.3.0)
>
> The second attempt was to use the code of
> http://www.guilmo.com/fmdb-with-sqlcipher-tutorial/. In this attempt i
> did not load the sqlcyher from the cocoapods like the tutorial. i just
> used his code to encrypt the database (the part below "Working the magic
> of SQLCipher")
>
> Met vriendelijke groet,
>
> Nico van Zijl
>
>
> 2014-07-25 16:04 GMT+02:00 Nick Parker <npa...@zetetic.net
> <mailto:npa...@zetetic.net>>:
>
> Hello Nico,
>
> Ok, great - that is a good sign! Next, do you know what version of
> SQLCipher was used to create the encrypted database you are trying to
> open with FMDB? Have you also tried to open the encrypted database via
> the SQLCipher shell? Are you certain the database was encrypted with
> SQLCipher to begin with?
>
> On 7/25/14 8:55 AM, Nico VZ wrote:
> > Hi Nick,
> >
> > When i run the query i get the following result: *3.1.0*
> > Seems to be correct right?
> >
> > Met vriendelijke groet,
> >
> > Nico van Zijl
> >
> >
> > 2014-07-25 15:08 GMT+02:00 Nick Parker <npa...@zetetic.net
> <mailto:npa...@zetetic.net>
> > <mailto:npa...@zetetic.net <mailto:npa...@zetetic.net>>>:
> > <mailto:sqlcipher%2Bunsu...@googlegroups.com
> <mailto:sqlcipher%252Buns...@googlegroups.com>>
> > > <mailto:sqlcipher+...@googlegroups.com
> <mailto:sqlcipher%2Bunsu...@googlegroups.com>
> > <mailto:sqlcipher%2Bunsu...@googlegroups.com
> <mailto:sqlcipher%252Buns...@googlegroups.com>>>.
signature.asc

William Gray

unread,
Aug 13, 2014, 10:31:33 AM8/13/14
to sqlc...@googlegroups.com
Hi Nico,

It's not clear to me when you run the code below which NSLog statement is printed, is setKey: returning YES or NO? If it's returning NO, you might try stepping into the call in the debugger to see whether it's properly executing at all—the method's implementation is surrounded by and #ifdef which won't run the code you want unless SQLCIPHER_HAS_CODEC is defined in your project's CFLAGS:


Hope that helps,
Billy

--

---
You received this message because you are subscribed to the Google Groups "SQLCipher Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlcipher+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
<Schermafbeelding 2014-07-24 om 09.17.45.png>

Reply all
Reply to author
Forward
0 new messages