sqlite - iOS sqlcipher fmdb “File is encrypted or is not a database” -
this tutorial works fine following pieces of code.
pod 'fmdb/sqlcipher'
...
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { ... nsarray *documentpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentdir = [documentpaths objectatindex:0]; self.databasepath = [documentdir stringbyappendingpathcomponent:@"gamedefault.sqlite"]; [self createandcheckdatabase]; ... }
...
-(void) createandcheckdatabase { bool success; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; success = [filemanager fileexistsatpath:self.databasepath]; if(success) return; // if file exists, dont // if file not exist, make copy of 1 in resources folder nsstring *databasepathfromapp = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@"gamedefault.sqlite"]; // file path [filemanager copyitematpath:databasepathfromapp topath:self.databasepath error:nil]; // make copy of file in documents folder // set new encrypted database path in documents folder nsarray *documentpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentdir = [documentpaths objectatindex:0]; nsstring *ecdb = [documentdir stringbyappendingpathcomponent:@"encrypted.sqlite"]; // sql query. note database full path not name const char* sqlq = [[nsstring stringwithformat:@"attach database '%@' encrypted key 'secretkey';",ecdb] utf8string]; sqlite3 *unencrypted_db; if (sqlite3_open([self.databasepath utf8string], &unencrypted_db) == sqlite_ok) { // attach empty encrypted database unencrypted database sqlite3_exec(unencrypted_db, sqlq, null, null, null); // export database sqlite3_exec(unencrypted_db, "select sqlcipher_export('encrypted');", null, null, null); // detach encrypted database sqlite3_exec(unencrypted_db, "detach database encrypted;", null, null, null); sqlite3_close(unencrypted_db); } else { sqlite3_close(unencrypted_db); nsassert1(no, @"failed open database message '%s'.", sqlite3_errmsg(unencrypted_db)); } self.databasepath = [documentdir stringbyappendingpathcomponent:@"encrypted.sqlite"]; }
...
[db setkey:@"secretkey"]
...
// fmdatabase fmdatabase *db = [fmdatabase databasewithpath:[self getdatabasepath]]; [db open]; [db setkey:@"secretkey"]; // fmdatabasequeue fmdatabasequeue *queue = [fmdatabasequeue databasequeuewithpath:[self getdatabasepath]]; [queue indatabase:^(fmdatabase *db) { [db setkey:@"secretkey"]; ... }];
...
when use same code in other project works fine encrypting existing sqlite database. but when try access table via sql select query error, "file encrypted or not database". though, no issue tutorial app , can update/insert/delete/select records there. clue?
i've fixed.i think there inter pod dependency fmdb & sqlcipher. have solved problem using below link. database file encrypt using sqlcipher fmdb.... :-d
Comments
Post a Comment