iosfmdbsqlcipher

FMDB & SQLCipher not encrypting


In Xcode I am trying to get FMDB to use SQLCipher to encrypt a database. In my project I already have a compiling version of SQLCipher which I already have proved is working via sqlite3 calls. I have a unit test that creates the database and 1 table then inserts a row. Everything works using FMDB except the database is still unencrypted.

-(id)initWithDatabaseFilename:(NSString*)filename{
NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
                          stringByAppendingPathComponent: filename];

self.databasePath = databasePath;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:databasePath]) {
    database = [FMDatabase databaseWithPath:databasePath];
    [database setLogsErrors:YES];
    [database setTraceExecution:NO];
    BOOL keyCheck = [database setKey:@"B!GSecret"];
    NSLog(@"Database is encrypted: %d",keyCheck);
    NSLog(@"database created");
} else {
    NSLog(@"Didnt need to create database");
}
   [self createTable];

return self;

}

-(void)createTable{

BOOL tableExists = [self.database tableExists:kTASKTableName];

if(!tableExists){
    [database open];
    [database executeUpdate:@"CREATE TABLE TEST(TEST_PK integer primary key autoincrement, TITLE text, OTHERTITLE text, TESTVAL text, COMPLETED integer, CREATION_DATE double)"];

    [database close];
}

}

-(BOOL) addTasks:(NSArray*) tasks{
BOOL insertSuccess = NO;

if([self.databasePath isEqualToString:@""]){
    NSLog(@"Database has not yet been initialized");
}
[database open]; 
for(TESTOBJ *to in tasks){


    [database executeUpdate:@"insert into TEST(TITLE, OTHERTITLE, TESTVAL) VALUES (?,?,?)",
     to.title,to.otherTitle,to.testVal,nil];
}
[database close];

return insertSuccess;

}


Solution

  • Sorted the issue by adding

    [database setKey:@"B!GSecret"];
    

    after each database open statement.