I had a lot of problems getting my SQLITE database encrypted for my project, and finally I'm trying to use the attach database method to encrypt my unencrypted database.
I tried running the attach command on Terminal, only to implement the output, there will be an unencrypted database. So, in truth, I have to run commands in my project, since my sqlcipher and ssl libraries are imported correctly?
So I tried, the method works without any glitches, but I did not even get the encrypted database that will be created in the document directory. What am I doing wrong? [Snapshot code below]
- (void)encryptDB
{
NSLog (@"Start");
sqlite3 *DB = [iPad_3AppDelegate getNewDBConnection];
sqlite3_exec(DB, "ATTACH DATABASE KeyCryptENC.db AS encrypted KEY 1234;", NULL, NULL, NULL);
sqlite3_exec(DB, "CREATE TABLE encrypted.Account(A_id,C_ID,Account_Name,Username,Password,Other_Information);", NULL, NULL, NULL);
sqlite3_exec(DB, "CREATE TABLE encrypted.Categories(C_ID,Category);", NULL, NULL, NULL);
sqlite3_exec(DB, "CREATE TABLE encrypted.UserInfo(Password,Hint,Secret_Question,Secret_Answer);", NULL, NULL, NULL);
sqlite3_exec(DB, "INSERT INTO encrypted.Account SELECT * FROM Account;", NULL, NULL, NULL);
sqlite3_exec(DB, "INSERT INTO encrypted.Categories SELECT * FROM Categories;", NULL, NULL, NULL);
sqlite3_exec(DB, "INSERT INTO encrypted.UserInfo SELECT * FROM UserInfo;", NULL, NULL, NULL);
sqlite3_exec(DB, "DETACH DATABASE encrypted;", NULL, NULL, NULL);
NSLog (@"End");
}
+ (sqlite3 *)getNewDBConnection{
sqlite3 *newDBconnection;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"KeyCrypt.sqlite"];
if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
NSLog(@"Database Successfully Opened :)");
} else {
NSLog(@"Error in opening database :(");
}
return newDBconnection;
}
Thanks so much for helping the guys!