magic number for SQLCipher files?

60 views
Skip to first unread message

Sean Hogan

unread,
Jun 21, 2012, 2:23:21 PM6/21/12
to sqlc...@googlegroups.com
Since I'm batting .000 in my first two at-bats, this time I'll just ask
without trying to guess the answer:

Is there a way to know that a given file is a SQLCipher database without
trying to open it with the correct passphrase?

Put differently, can SQLCipher differentiate a bad passphrase from the
case of a non-database file supplied to it? If so, is that method open
to other applications?

Thanks again,
Sean

Nick Parker

unread,
Jun 21, 2012, 3:03:25 PM6/21/12
to sqlc...@googlegroups.com, Sean Hogan
Hi Sean,

In a scenario where you are attempting to open a SQLCipher database, but
are unsure if it was encrypted with a password you could perform a query
such as:

select count(*) from sqlite_master;

In the event that you did not provide a PRAGMA key = '...', but the
database was encrypted you would get an error back saying:

Error: file is encrypted or is not a database.

Note, if you were to provide a PRAGMA key = '...', however the key value
is incorrect, you will get the same error message. Thus, getting a
result back without an error in the first situation would validate the
database is in fact unencrypted and the second case identifies an
invalid password.
--
Nick Parker



Sean Hogan

unread,
Jun 21, 2012, 3:18:48 PM6/21/12
to Nick Parker, sqlc...@googlegroups.com
Thanks Nick,

Let me clarify that I'm using the SQLCipher for Android Java API and openOrCreateDatabase.  I'm going to assume (cringe) that I don't have access to the error message string you mentioned.

I think you're saying that:
  1. If I specify an empty password and get back a database reference, it means the database is unencrypted.
  2. If I specify an empty password and get back a null reference, then the database is either invalid or encrypted.
  3. If I specify a non-empty, incorrect password and get back a null reference, then the database is encrypted but the password is invalid.

What happens if some random file (not a SQLCipher or SQLite database) is supplied to openOrCreateDatabase... wouldn't it look the same as cases 2 or 3?

Cheers,
Sean

Nick Parker

unread,
Jun 21, 2012, 3:27:37 PM6/21/12
to Sean Hogan, sqlc...@googlegroups.com
Hi Sean,

In the case of SQLCipher for Android, when you call SQLiteDatabase.openOrCreateDatabase(...), you will get a SQLiteException thrown when that error condition is reached.  To your last question, if that file did not exist it would simply create a database with the supplied password, otherwise you would get a SQLiteException.
-- 
Nick Parker


Reply all
Reply to author
Forward
0 new messages