I have been trying to encrypt/ decrypt a sqlite database in my iPhone project. I am able to encrypt the database by using the reKey method. But I am unable to decrypt it. I have kept my sqlite.db file in a folder. Currently trying it on a simulator.
Code snippet:
[[SQLiteDB sharedSQLiteDB] open:<path to the folder> withKey:@""];
[[SQLiteDB sharedSQLiteDB] reKey:@"abc"];
[[SQLiteDB sharedSQLiteDB] close];
[[SQLiteDB sharedSQLiteDB] open:<path to the folder> withKey:@"abc"];
[[SQLiteDB sharedSQLiteDB] reKey:@""];
.....
- (BOOL)open:(NSString *)path withKey:(NSString *)masterKey {
if (sqlite3_open([path fileSystemRepresentation], &_db) != SQLITE_OK) {
NSLog(@"SQLite Opening Error: %s", sqlite3_errmsg(_db));
return NO;
}
if(masterKey)
sqlite3_exec(_db, [[NSString stringWithFormat:@"PRAGMA key = '%@'", masterKey] UTF8String], NULL, NULL, NULL);
if (sqlite3_exec(_db, (const char*) "SELECT count(*) FROM sqlite_master", NULL, NULL, NULL) != SQLITE_OK)
{
[self close];
NSLog(@"SQLite Key Error!");
return NO;
}
filePath = [path retain];
return YES;
}
......
- (void)reKey:(NSString *)masterKey
{
sqlite3_exec(_db, [[NSString stringWithFormat:@"PRAGMA rekey = '%@'", masterKey] UTF8String], NULL, NULL, NULL);
}
I have read the posts on this topic in sqlcipher google groups, but I am unable to decrypt it. Any help would be highly appreciated.