I have had many 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've tried running the attach command on Terminal, only to realise the output would be a unencrypted database. So by right i'm supposed to run the commands in my project, with my sqlcipher and ssl libraries imported right?
So I tried it, the method runs without any faults, but i didn't even get the encrypted database to be created in the documents directory. What am I doing wrong? [Code Snippet 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"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
NSLog(@"Database Successfully Opened :)");
} else {
NSLog(@"Error in opening database :(");
}
return newDBconnection;
}
Many thanks for the help guys!
-
Hi seelani, can you explain in detail, how you managed to create an encrypted database from a plain-text sqlite database ? I am just stuck as you wereuser570010– user5700102011年01月10日 18:54:01 +00:00Commented Jan 10, 2011 at 18:54
-
1have you figured it out, sorry for the late reply was enjoying a week of holidays. well this is the link i used to solve my issue, github.com/sjlombardo/sqlcipher/issues/closed#issue/1 look to the second comment by sjlombardo.seelani– seelani2011年01月18日 06:14:28 +00:00Commented Jan 18, 2011 at 6:14
-
Did KeyCryptENC.db have to exist prior to executing that code? Should KeyCryptENC.db be saved to the Documents directory for the app?Kevin_TA– Kevin_TA2012年01月19日 18:22:33 +00:00Commented Jan 19, 2012 at 18:22
-
@seelani: Above link is broken and I am facing the same issue so can you correct it or explain it more?Iducool– Iducool2012年07月05日 05:19:36 +00:00Commented Jul 5, 2012 at 5:19
1 Answer 1
after more intensive research I realised the problem to the encryption, ME. I was using Mac OS X native sqlite3. And yeah this link should help anyone who faces a problem similar to mine.
BWAHAHA, i feel so silly for not thinking of this earlier.