Hi, I am trying to decrypt the in iOS objective c
.db file but get error file is not a database.
Tried so many methods.
By opening DB I set the key but not work.
Here I also write some code for decryption but not getting work . Please help me out.
dbData = [NSData dataWithContentsOfFile:filePath];/// ENCRYPTED DB SAVE
//////DECRYPTION PART//////
NSError *error;
NSString *stringToWrite = @"";
NSString *filPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"plainText.db"];
[stringToWrite writeToFile:filPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
// NSString *filPath = [documentsPath stringByAppendingPathComponent:@"plainText.db"];
sqlite3 *db;
sqlite3_stmt *stmt;
NSString *masterKey = @"abc";
int rc;
if (sqlite3_open([filePath UTF8String], &db) == SQLITE_OK) {
NSLog(@"Database open successfully.");
sqlite3_exec(db, [[NSString stringWithFormat:@"PRAGMA rekey = '%@'", masterKey] UTF8String], NULL, NULL, NULL);
}
rc = sqlite3_prepare(db, "PRAGMA cipher_version;", -1, &stmt, NULL);
if (rc != SQLITE_OK){
NSLog(@"error");
}
const char* sqlQ = [[NSString stringWithFormat:@"ATTACH DATABASE '%@' AS plainText KEY '';", filPath] UTF8String];
if (sqlite3_exec(db, sqlQ, NULL, NULL, NULL)== SQLITE_OK){
if (sqlite3_exec(db, "SELECT sqlcipher_export('plainText');", NULL, NULL, NULL) == SQLITE_OK){
// if (sqlite3_exec(db, "DETACH DATABASE plainText;", NULL, NULL, NULL)== SQLITE_OK){
NSLog(@"everythimng ok");
sqlite3_stmt* statement;
NSString *query = @"SELECT name FROM sqlite_master WHERE type='table'";
int retVal = sqlite3_prepare_v2(db,
[query UTF8String],
-1,
&statement,
NULL);
NSMutableArray *selectedRecords = [NSMutableArray array];
if (SQLITE_OK != retVal) {
NSString *s = [NSString stringWithFormat:@"%s", sqlite3_errmsg(db)];
}
if ( retVal == SQLITE_OK )
{
while(sqlite3_step(statement) == SQLITE_ROW )
{
NSString *value = [NSString stringWithCString:(const char *)sqlite3_column_text(statement, 0)
encoding:NSUTF8StringEncoding];
[selectedRecords addObject:value];
}
}
else {
NSString *s = [NSString stringWithFormat:@"%s", sqlite3_errmsg(db)];
NSLog(@"%@", s);
}
sqlite3_clear_bindings(statement);
sqlite3_finalize(statement);
NSLog(@"%@", selectedRecords);
//[self alterCiperDB_test];
// }
// else {
// NSString *s = [NSString stringWithFormat:@"%s", sqlite3_errmsg(db)];
//
// //let errmsg = String(cString: sqlite3_errmsg(db))
// NSLog(@"%@", s);
// }
}
else {
NSString *s = [NSString stringWithFormat:@"%s", sqlite3_errmsg(db)];
//let errmsg = String(cString: sqlite3_errmsg(db))
NSLog(@"%@", s);
}
}
else {
//NSString *s ;
NSString *s = [NSString stringWithFormat:@"%s", sqlite3_errmsg(db)];
//let errmsg = String(cString: sqlite3_errmsg(db))
NSLog(@"%@", s);
}