0

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!

MPelletier
16.8k18 gold badges91 silver badges140 bronze badges
asked Dec 6, 2010 at 4:45
4
  • 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 were Commented Jan 10, 2011 at 18:54
  • 1
    have 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. Commented 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? Commented 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? Commented Jul 5, 2012 at 5:19

1 Answer 1

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.

answered Dec 9, 2010 at 7:35
Sign up to request clarification or add additional context in comments.

3 Comments

Hi seelani, can you tell me from where the sqlite for sqlciper to use ? I am alos having the same issue.
You should use sqlite from the source directory of sqlcipher. You can find the detailed description by sjlombardo in the 2nd comment of the issue #1: github.com/sjlombardo/sqlcipher/issues/1
Because we moved sqlcipher to its own organization on GitHub, the links to issues have changed. I think this is the comment you folks were linking to: github.com/sqlcipher/sqlcipher/issues/1#issuecomment-24738

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.