Unable to encrypt DB using SQLCipher

Hi there,

I followed all the instruction from tutorial link. But still, I was unable to encrypt my database.

I am able to open the DB and perform all the operation on it.

There is one scenario in which I am getting a different result.

  1. When DB is empty (there is no table). In this case when I am executing the given command then it executes all the commands i.e
NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
                          stringByAppendingPathComponent: @"sqlcipher.db"];
sqlite3 *db;
sqlite3_stmt *stmt;
bool sqlcipher_valid = NO;

if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) {
    const char* key = [@"BIGSecret" UTF8String];
    sqlite3_key(db, key, strlen(key));
    if (sqlite3_exec(db, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
      if(sqlite3_prepare_v2(db, "PRAGMA cipher_version;", -1, &stmt, NULL) == SQLITE_OK) {
        if(sqlite3_step(stmt)== SQLITE_ROW) {
          const unsigned char *ver = sqlite3_column_text(stmt, 0);
          if(ver != NULL) {
            sqlcipher_valid = YES;

            // password is correct (or database initialize), and verified to be using sqlcipher

          }
        }
        sqlite3_finalize(stmt);
      }
    }
    sqlite3_close(db);
}

It moves to the inner loop and set the bool sqlcipher_valid to true. But after this when I opened DB with SQLite manager then I am able to perform the operation. 

--------------------------------------------

2) If DB is not empty (having tables). In this case when the following command is executed then it through error code 26 which means its not a DB file. 

Command: sqlite3_exec(db, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK

Kindly look into this and let me know the solution.

Thanks in Advamce !!

Hi @Pankaj_Sharma,

Thank you for your interest in SQLCipher and for posting to the discussion forum.

A couple of comments about the results you are seeing:

  1. When DB is empty (there is no table). In this case when I am executing the given command then it executes all the commands i.e

    It moves to the inner loop and set the bool sqlcipher_valid to true. But after this when I opened DB with SQLite manager then I am able to perform the operation.

SQLCipher doesn’t encrypt the database until data is added to the database. Until that point it will still be able to be opened without a password.

  1. If DB is not empty (having tables). In this case when the following command is executed then it through error code 26 which means its not a DB file.

Do you mean you have a plaintext SQLite database and are attempting to encrypt it?

If you’re attempting to encrypt an existing plaintext SQLite database, you’ll want to have a look at the sqlcipher_export() convenience function

Hi @mmoore,

Thanks for replying.

I am using SQLCipher for iOS. I am trying to encrypt DB through code provided.

I am trying to encrypt already exist DB. All command executed but when reaches to command ```
sqlite3_exec(db, (const char*) “SELECT count(*) FROM sqlite_master;”, NULL, NULL, NULL) == SQLITE_OK


It will through error code 26 which means file is not a DB file. I am doing all thing through XCode only. 

Thanks !!

@Pankaj_Sharma

Please read the last part of my response again:

Do you mean you have a plaintext SQLite database and are attempting to encrypt it?
If you’re attempting to encrypt an existing plaintext SQLite database, you’ll want to have a look at the sqlcipher_export() 1 convenience function

The code you posted won’t work for encrypting an already existing SQLite database. You need to sqlite3_exec the command shown in the sqlcipher_export() post to be able to encrypt a plain text SQLite database.

There’s more explanation here: How to encrypt a plaintext SQLite database to use SQLCipher (and avoid “file is encrypted or is not a database” errors)

Cheers,
Micah

Hi @mmoore
Thanks for the reply. It’s working now.

@Pankaj_Sharma

Glad to hear everything is working now.